Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is it giving error on math.random?

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)

2 comments

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.

0

Reread it.

1 answer

✓ Accepted answer

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.