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

My basic script for a new "game" I'm making isn't working. Does anyone know why, or have a solution?

game.Players.PlayerAdded:Connect(function(plr)
    local PCs = Instance.new("Folder",plr)
	PCs.Name = "PCs"
end)

function createPC()
	local PC = Instance.new("IntValue", game.Players.LocalPlayer:WaitForChild("PCs"))
	PC.Name = tostring(game.Players.LocalPlayer.PCs:GetChildren() + 1)
end

createPC()

Ther error happnes on line 7 "local PC = Instan..." Here is the error: 15:07:00.232 - ServerScriptService.Script:7: attempt to index nil with 'WaitForChild'

1 answer

✓ Accepted answer

LocalPlayer cannot be used in server scripts, only local scripts only

It's quite a simple fix though

game:GetService("Players").PlayerAdded:Connect(function(plr)
    local PCs = Instance.new("Folder",plr) --Create the folder
    PCs.Name = "PCs"
    local newPC = Instance.new("IntValue", PCs)
    local value = 0
    for _,v in pairs(PCs) do
    	if nil then value = 1 --or 0, I don't know what you want
    else
          value = value + 1
    end
    newPC.Name = value --I think this is what you want?   
end)

Everytime a player joins with the PlayerAdded event, a new folder inside them called PCs will be created. Then a new value. I'm not sure what you're doing but I hope this works for you!

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