Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

Why is it giving error on math.random?

Asked by 9 years ago
01pants = script.Parent.Pants:GetChildren()
02shirts = script.Parent.shirts:GetChildren()
03faces = script.Parent.Faces:GetChildren()
04hairs = script.Parent.Hair:GetChildren()
05maindummy = game.Workspace.Camera.Dummy_3D
06dummy = script.Parent.Dummy
07repeat wait() until maindummy and dummy and hairs and faces and shirts and pants
08 
09script.Parent["Pant Label"].Next.MouseButton1Click:connect(function()
10local lastSpawn = nil  
11    repeat
12    local curSpawn = pants[math.random(1,#pants)]
13    until curSpawn ~= lastSpawn
14            lastSpawn = curSpawn
15    wait() --Let the Character fully load
16    maindummy.Pants.PantsTemplate = "http://www.roblox.com/asset/?id="..curSpawn 
17    dummy.Pants.PantsTemplate = maindummy.Pants.PantsTemplate
18end)

line 16 is errored, Players.Player.PlayerGui.CreateChar.CharChange:16: attempt to concatenate global 'curSpawn' (a nil value)

0
Are you meaning to grab the template from the faces to use on the pants? Because that's what it looks like in line 12. Pyrondon 2089 — 9y
0
Reread it. BuilderCosmic 43 — 9y

1 answer

Log in to vote
1
Answered by
Pyrondon 2089 Game Jam Winner Moderation Voter Community Moderator
9 years ago

On line 12, you need to remove 'local'. Using local means that the variable is only stored in that scope -- The repeat block. You need it outside of the repeat block, so it needs to be global. You may also want to set the variable earlier on in the script, not sure if necessary but I can't recall if it isn't.

So, the script:

EDIT: Also, you may want to move the "local lastSpawn = nil" to earlier in the script. The way you have it set up, it will set it to nil every time the button is clicked, which I assume is not the desired effect. If you want it to be a different one each time, you'll want to move it to earlier in the script.

01curSpawn = nil
02-- lastSpawn = nil -- Edit: This is what I mean. Add this..
03pants = script.Parent.Pants:GetChildren()
04shirts = script.Parent.shirts:GetChildren()
05faces = script.Parent.Faces:GetChildren()
06hairs = script.Parent.Hair:GetChildren()
07maindummy = game.Workspace.Camera.Dummy_3D
08dummy = script.Parent.Dummy
09repeat wait() until maindummy and dummy and hairs and faces and shirts and pants
10 
11script.Parent["Pant Label"].Next.MouseButton1Click:connect(function()
12local lastSpawn = nil   -- Edit: ..And remove this.
13    repeat
14        curSpawn = pants[math.random(1,#pants)]
15    until curSpawn ~= lastSpawn
16    lastSpawn = curSpawn
17    wait() --Let the Character fully load
18    maindummy.Pants.PantsTemplate = "http://www.roblox.com/asset/?id="..curSpawn 
19    dummy.Pants.PantsTemplate = maindummy.Pants.PantsTemplate
20end)
Ad

Answer this question