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 8 years ago
pants = script.Parent.Pants:GetChildren()
shirts = script.Parent.shirts:GetChildren()
faces = script.Parent.Faces:GetChildren()
hairs = script.Parent.Hair:GetChildren()
maindummy = game.Workspace.Camera.Dummy_3D
dummy = script.Parent.Dummy
repeat wait() until maindummy and dummy and hairs and faces and shirts and pants

script.Parent["Pant Label"].Next.MouseButton1Click:connect(function()
local lastSpawn = nil   
    repeat
    local curSpawn = pants[math.random(1,#pants)]
    until curSpawn ~= lastSpawn
            lastSpawn = curSpawn
    wait() --Let the Character fully load
    maindummy.Pants.PantsTemplate = "http://www.roblox.com/asset/?id="..curSpawn  
    dummy.Pants.PantsTemplate = maindummy.Pants.PantsTemplate
end)

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 — 8y
0
Reread it. BuilderCosmic 43 — 8y

1 answer

Log in to vote
1
Answered by
Pyrondon 2089 Game Jam Winner Moderation Voter Community Moderator
8 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.

curSpawn = nil 
-- lastSpawn = nil -- Edit: This is what I mean. Add this..
pants = script.Parent.Pants:GetChildren()
shirts = script.Parent.shirts:GetChildren()
faces = script.Parent.Faces:GetChildren()
hairs = script.Parent.Hair:GetChildren()
maindummy = game.Workspace.Camera.Dummy_3D
dummy = script.Parent.Dummy
repeat wait() until maindummy and dummy and hairs and faces and shirts and pants

script.Parent["Pant Label"].Next.MouseButton1Click:connect(function()
local lastSpawn = nil   -- Edit: ..And remove this.
    repeat
        curSpawn = pants[math.random(1,#pants)]
    until curSpawn ~= lastSpawn
    lastSpawn = curSpawn
    wait() --Let the Character fully load
    maindummy.Pants.PantsTemplate = "http://www.roblox.com/asset/?id="..curSpawn  
    dummy.Pants.PantsTemplate = maindummy.Pants.PantsTemplate
end)
Ad

Answer this question