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

Error While Testing It using Server Client?

I am testing this hat GUI giver thing and it works perfectly fine in studio test, while I test it in server client though... It gives me this error and does not delete the hat off of the character while clicking the arrow:

attempt to index upvalue "character" (a nil value)

here is the line of code where the error is coming from:


local character = game.Players.LocalPlayer.Character
local hat = game.Players.LocalPlayer.Hat




hat.Changed:Connect(function()
	for i, v in pairs(character:GetChildren()) do
		if v.ClassName == "Hat" then
			v:Destroy()
		end
	end
end)

2 comments

0

You should add findifrstchild and if hat then first, these small things can fix a script.

0

Doesn't work...

1 answer

✓ Accepted answer

LocalScripts first run as soon as the Roblox client opens so you should not expect anything to be loaded. This includes characters. Good practice:

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local hat = player:WaitForChild("Hat")

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