#How to create a reusable model with logic?

1 messages · Page 1 of 1 (latest)

nimble helm
#

Hi you all. I am a person who craves reusability and is constantly looking for a pain in the ass way to do something that will save me time later on.

I am trying to make a simple obby as a first project and I need a "Buy_part" for this. It is basically a part that the player touches to buy an assigned model in the form of [Trees, stairs, buildings, etc.] now I am not that experienced with Luau, yet! And I require the help of some of them nerd out here 🤭 (No offense intended)

I heard there is a way for the scripts to acces some sort of storage hold for models and spawn them in via this, maybe this is one of the things I will need to build an obby but I am not sure yet. Mainly I am looking to a way to make this "Buy_part" and being able to just put it into any model I want to make it act as a "Buyable".

Here are a way I came up with;
The script inside the Buy_part model will dig into the model its a child off and look for the IntValue part inside of it called "Price" to know how expensive the model is. Next it will connect the touhced signal and look for humanoid n all, not too hard, the hardest part is mainly for the script to know what its parent is.
I dont know of any ways to just acces the script's parent as doing game.workspace.[model_name].script will probably not work as I will be copying this buy_part model, script included, into many different models so they can act as a "Buyable" and this will break the path.

Can somebody explain to me how this is done? Is my current way of thinking the right conclusion? Or is there a better and easier way to make this happen?
(I'm an advanced GD-Script user so trying out Luau is a big step as I only know programmatic thinking in Luau)

#

I thank this community in advance 🙏

sharp ruin
#

i don't know what you trying to make here

nimble helm
#

In a nutshell I want to have a script that I can add to any model to turn them into a buyable model

#

Imo having to copy paste the code for every model just seems tedious

sharp ruin
#

yeah

nimble helm
#

so I was wondering how I could make it more effective and just copy a script in or smth

sharp ruin
#

have you tried using collection service

nimble helm
#

(Im like super new in Luau. I know how the language works, kinda, I just need to start getting used to the way scripts and other instances interact with one another in roblox studios)

nimble helm
#

Like I said I know very little about the studio set-up wise

#

I just have some knowledge about the language

sharp ruin
#

okay so collection service is designed to find all tags and you can add tag every instance (lets say the buy model)

#

and then you can have one script somewhere

#

get collection service to get all tagged that you have added in a for i, v

#

i'll show you an example hold on

nimble helm
nimble helm
sharp ruin
#

somewhere below properties you can find a tag menu where you can either delete or add, you name the tag whatever you've liked

#

you can have multiple tags

nimble helm
#

Ah oke

sharp ruin
#

not sure how many i have never done 3+ tags

nimble helm
#

As that’s the ultimate goal here. Use one script to do things for more than one model

sharp ruin
#

i hope you understand i'm not good at teaching 😭

nimble helm
#

Alright…
So I just put whatever code I would have done in every script under this loop?

sharp ruin
#

yeah

nimble helm
#

But what if I want an event to be done via this?

#

U can put events in loop as they yk…dont loop

#

Do I just connect their event to a function in the loop?

nimble helm
sharp ruin
#

yeah you can connect an event to each part

nimble helm
#

Oh and how can I refer to the part inside of the touched function?

#

As I need the part to acces it’s parent for the price instance

nimble helm
timber shellBOT
#

studio** You are now Level 3! **studio

sharp ruin
#

i would do this rather than having to access the part's parent

#

if you have a script inside model, i would do script.Parent

#

but collection service is nicer method to do this

nimble helm
#

Wait does this loop create some sort of hold for this information?
Like how is TouchedPart for “part1” still gonna equal part1 when it goes to the next iteration?

nimble helm
#

I’ll give it a try in studios and see if it does what I want it to do :3

#

Where should I place such a script tho?

#

@sharp ruin

sharp ruin
sharp ruin
nimble helm
#

Btw can you tell me how the for loop works?

#

like what do the variables/numbers represent

nimble helm
#

(N should It be a normal script?)

sharp ruin
# nimble helm What that do?

serverscriptservice is where server scripts should be but everywhere can be optional however serverscriptservice couldn't be accessed by the local client

#

it's more of a

#

vault

nimble helm
#

Thanks for explaining all this by the way 🙏

sharp ruin
#

vault for server side

sharp ruin
sharp ruin
nimble helm
#

You put a few arguments after the for loop

#

What do they do

#

As for loops act a lil bit diff than the other language I used 😊

#

You do “a, b in c”

#

What are these three parameters?

sharp ruin
#

oh you mean

#

_, Part in Pairs

nimble helm
#

So i probably can’t refer to it

nimble helm
#

What does _do?

sharp ruin
#

i put _ because the GetTagged() returns a table

#

and the table would looked like

#

[1] = tagged_instance

#

[1] is a Index

nimble helm
#

Yeh

#

I know 🙂

nimble helm
#

Am I supposed to use a different script than just normal script?

#

As it doesn’t seem to recognize collection services

sharp ruin
#

did you getservice first

timber shellBOT
#

studio** You are now Level 4! **studio

nimble helm
#

Mb

nimble helm
nimble helm
# sharp ruin oh you mean

Still can you explain why the use are for these three parameters?
Like what is the function of the first one, the second one and the one after in

sharp ruin
#

_ is something i put because it's just numbers based on the table so i couldn't do much with it so i put it as a blank, Model: Model the reason why it has : Model because it's a type check to make sure the Model is actually "Model" so i can have the intellisense of that instance

pairs is more like normal one by one, there was the other one which is ipairs with extra "i" that goes one by one only if the index on the table are numbers like [1] to [5], it can be used if you made a table and you want a specific first part to go exact second part

nimble helm
#

Okidoki

#

Now I got one more question 🤭

#

Will doing “game.Player:findfirstchild({the name of the part of the player.Parent.Name})” return to me the player’s instance inside of the players tab?

#

For more clarification, read the code 🙂

sharp ruin
#

it would work maybe but just to be sure you can do game.Players:GetPlayerFromCharacter(humanoid.Parent) which also returns the player

nimble helm
#

Ooooh

#

That’s like so much easier

#

Thanks

sharp ruin
#

i would recommend instead of int value for price

#

add number attribute on model

#

its right next to tag menu

#

and get Model:GetAttribute("Price") to get the price value

nimble helm
#

Ayo this engine is cool

#

Thanks 🙂
cant believe the community for developers is like non-toxic while the players are all greedy self obsessed people

#

I appreciate you helping me, sir 😄

#

@sharp ruin can I also do a while loop in this script?

timber shellBOT
#

studio** You are now Level 4! **studio

sharp ruin
#

if you do loop in a for i, v it's gonna

nimble helm
#

As I kinda want the parts to go green when you have enough money and go red when you don’t

sharp ruin
#

stop right there

#

you can do
task.spawn(function()
-- loop here
end)
inside of for i, v in pairs

to have next one going

sharp ruin
#

task.spawn creates a thread you can cancel if you have the thread in local thread = task.spawn()

#

task.cancel()

nimble helm
#

Huh…

#

Is there an easier way to make them go green n red depending on the money?

sharp ruin
nimble helm
#

No we don’t want that

sharp ruin
#

task.spawn() essentially is like creating a hole in the wall so the rest of the code still runs

nimble helm
#

Heard about threads. They’re like the “paths” the code runs on. So if the loop and while are on different paths the won’t interfere with each other

sharp ruin
#

if you put while loop in the for i v pairs the next won't be happening because the first iteration is yielding

#

the code stucks there

nimble helm
sharp ruin
nimble helm
#

If he hasn’t it’s red

#

I figured doing a while for all of them constantly checking the price against his money would do it

sharp ruin
#

the tile part of the buy model?

nimble helm
sharp ruin
#

oh

#

why are you putting while loop when touched event is there

nimble helm
#

It’s like the standard obby
If you can buy it it’s green

nimble helm
#

How would you create this?

sharp ruin
#

oh you mean if the player has enough money the tile turns green

#

sorry

nimble helm
#

I have to go to the major for an appointment. So if you have any information regarding the green red mechanic please text it as I will read it in 40 or so minutes 😄

sharp ruin
#

it's getting late i gotta sleep