|
| 05 Aug 2017 03:58 PM |
Hey guys I'm really stuck with a script..
I want a local script that triggers when the player enters and it causes a random part from a model in server storage to change its name and clone itself to workspace..
local player = script.Parent.Parent local id = player.userId local plates = game.ServerStorage.Plates local availbale = plates:GetChildren()
if availbale ~= nil then local random = plates:GetChildren()[math.random(#plates:GetChildren())] local spawn_location = plates:FindFirstChild(random.Name) print(spawn_location) end
The script will print the name of the part it has selected but I cant make it do anything else.. Once the script had finished I was going to remove the origional part from serverstorage to make sure the math function didn't select the same part twice
Would anybody be able to help me out? (Even if it's not an entire script) also if there's an easier way than the way I've tried (Which there probably is) could you let me know? :)
Thank you for your help!
|
|
|
| Report Abuse |
|
|
|
| 05 Aug 2017 04:08 PM |
Pretty much I need:
when a player enters if there are parts in a model in ServerStorage select a random part from the model then change the parts name, clone the part to workspace and delete the origional (the part in ServerStorage)
Please help me I'm pretty new to all of this, once you've told me i won't ask anything like this again tho dw :P I'm alright at deciphering code to reuse it elsewhere :D
Thank you! :) |
|
|
| Report Abuse |
|
|
doggy00
|
  |
| Joined: 11 Jan 2011 |
| Total Posts: 3571 |
|
|
| 05 Aug 2017 04:13 PM |
math.randomseed(tick()%1*1e6) --make things actually random
partclone = nil
model = game:GetService("ServerStorage"):WaitForChild("model name here")
ch = model:GetChildren()
n = math.random(1,#ch)
for i,v in pairs(ch) do
if i == n then
partclone = v:Clone() v:remove()
end
end
partclone.Name = "new name for cloned part here"
--do anything else you want to do with the cloned part here if you need to
partclone.Parent = workspace |
|
|
| Report Abuse |
|
|
|
| 05 Aug 2017 04:16 PM |
Haven't even tried it yet but im so excited that someone replied that im going to say thanks straight away :D :P
Gonna go test it out i'll let you know if everything went well :) (which ofc it will right? ;) :P )
|
|
|
| Report Abuse |
|
|
doggy00
|
  |
| Joined: 11 Jan 2011 |
| Total Posts: 3571 |
|
|
| 05 Aug 2017 04:22 PM |
| It isn't tested, but looking over it, as long as you put it in a server script and change the strings to their respective values, it should work fine. Happy to help! |
|
|
| Report Abuse |
|
|
|
| 05 Aug 2017 04:40 PM |
Working perfectly thank you! :)
I don't want to take too much of your time but would you be able to tell me if my understanding is right / let me ask a few questions? :) If not it's completely fine just reply "No sorry" so I'm not waiting for a reply :P
1) math.randomseed(tick()%1*1e6)
--This just generates a random number? Does this make the script happen at a random time?
2) partclone = nil model = game:GetService("ServerStorage"):WaitForChild("Plates") ch = model:GetChildren()
--Just variables?
3) n = math.random(1,#ch)
--Is this the part that chooses a random number between 1 and the number of children in the model?
4) for i,v in pairs(ch) do
--I've never understood this part of scripts: is it making a list? what does i,v represent? what does in pairs() do? and what is ch? :P Sorry that's kind of a long one :D
5) if i == n then partclone = v:Clone() v:remove()
--I'll probabl understand this when #4 gets explained :D
Thanks again :)
|
|
|
| Report Abuse |
|
|
| |
|
|
| 05 Aug 2017 05:21 PM |
yeah and i literally dont understand the part after i,v i don't understand how the list thing works -.-
super annoying cos i bet its useful for literally everything
Can anyone explain? :P |
|
|
| Report Abuse |
|
|
doggy00
|
  |
| Joined: 11 Jan 2011 |
| Total Posts: 3571 |
|
|
| 06 Aug 2017 04:28 AM |
1. It's a seed which guarantees random numbers when using math.random(), math.random() itself usually doesn't spit out random numbers unless you give it a seed to do so. The one I used works on pretty much any occasion.
2. I put partclone = nil so it wouldn't break later down the script, but yes, that's a variable, and I also kinda messed up there, as I should've put an "if partclone then" line to prevent it from running if partclone is still nil. Other than that, 'model' is a variable defining the model object and ch is a variable defining the children (objects/instances) inside of the model.
3. Spot on.
4. 'for i,v in pairs(table) do' is something which runs through the table (in this case, the list of children inside the model acts as the table) in order. This is known as one of many loops, as it loops for however many values are inside the table (so in this case it'd loop N times, N being the number of the children in the table 'ch'). 'i' refers to the number of times it has looped already, so if it's on its first value, i will be 1, and so on. On the other hand, 'v' represents the value assigned to the table. This is probably still confusing, so I'll use an example.
Let's say you have a table that looks like this: tbl = {part1,part2,part3,part4}
if you were to do a 'for i,v in pairs' on this table, then when i == 1, v would be part1. If i == 2, then v would be part2, and so on.
In the script I gave you, 'i' is useful for picking a random part out of the model. First, it picks a random number from 1 to the number of values within the table (AKA the parts in the model). Then, it loops through each individual value with the 'for i,v in pairs' loop. It compares 'i' with the variable the random number was assigned to. This still may be confusing, so here's another example.
Using the same table above, let's do this:
n = math.random(1,#tbl)
For the sake of the example, let's say n happens to be 3, so n = 3.
Then let's do:
for i,v in pairs(tbl) do
if i == n then
--code
end
end
Here, it's asking if i is the same as n, so if i is 3, then do the rest of the code.
In other words, it will ignore the code for part1, part2, and part4, seeing they are in position 1, 2, and 4 of the table, so their i's would be 1, 2, and 4.
part3, however, will be affected by the code, as it's i is 3, the same number the math.random() selected.
Usually you probably wouldn't use i unless you were trying to pick something out of random, but it is useful for other things, too, I just can't think of anything else at the moment. Most of the time, you'd use 'for i,v in pairs(tablename) do' as a way to loop through a table and change each individual value by using 'v' to refer to the value.
ANYWAYS...
5. Yes, this is comparing 'i' to the random number to ensure the value in the table is in the same number position as the random number selected, making the selection truly random. Then, it clones the object using v and assigns the clone to a variable so you can refer to it later on. It then removes the original object.
Hope this helped, and excuse any spelling/grammar errors, I didn't feel like proofreading all of this :P |
|
|
| Report Abuse |
|
|
|
| 06 Aug 2017 07:19 AM |
Thank you sooo much, I feel like this has actually really helped I feel like I'm gonna be using the table code so often now :D
Thank you for taking so long on #4 I know it was hard to explain but I really appreciate it :)
What a legend :) :D |
|
|
| Report Abuse |
|
|
doggy00
|
  |
| Joined: 11 Jan 2011 |
| Total Posts: 3571 |
|
|
| 06 Aug 2017 03:33 PM |
Nice to know I'm capable of helping people :P
Also, if you have any questions you could always check the wiki (wiki.roblox.com), it has tutorials on a lot of different things, from basic scripting fundamentals to more advanced topics. I pretty much taught myself through that one website.
If you still aren't sure, though, feel free to ask here. As long as it isn't super broad or asking something extraordinarily long, people here will most likely respond within minutes. |
|
|
| Report Abuse |
|
|