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

Keep Getting an Error (Userdata value) but script is correct..?

I have it set-up in ReplicatedStorage with a folder called "game" and 2 parts inside. Inside of the workspace i have a part called "Base" and inside of it is a ClickDetector and this script. Why doesnt it work? Please help

local replicatedStorage = game:GetService("ReplicatedStorage")
local gamez = replicatedStorage:WaitForChild("game")

function Clicked()
	local pl = gamez:GetChildren()
	local selectedIndex = math.random(1,#gamez)
	local gmae = pl[selectedIndex]:Clone()
	gmae.Parent = game.Workspace
	gmae.Name = "Choosen"
	return pl[selectedIndex].Name
end
script.Parent.ClickDetector.MouseClick:connect(Clicked)

Here is the error

Workspace.Base.Script:6: attempt to get length of upvalue 'gamez' (a userdata value)

1 answer

✓ Accepted answer

I believe your error was on line 6. Here's the fix.

local replicatedStorage = game:GetService("ReplicatedStorage")
local gamez = replicatedStorage:WaitForChild("game")

function Clicked()
    local pl = gamez:GetChildren()
    local selectedIndex = math.random(1,#pl) -- You tried to get the 'gamez' from line 2
    local gmae = pl[selectedIndex]:Clone()
    gmae.Parent = game.Workspace
    gmae.Name = "Choosen"
    return pl[selectedIndex].Name
end
script.Parent.ClickDetector.MouseClick:connect(Clicked)

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