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 saying character is a nil value?

Hey I have a problem with a script (local script) that tells me Player.shaboogly123.PlayerGui.ChooseTeam.TeamJoin.JoinBlue.LocalScript:2: attempt to index field 'Character' (a nil value) when I'm in an actual game server, but in studio it works fine. The actual script is:

local player = game.Players.LocalPlayer
local torso = player.Character:WaitForChild("Torso")
script.Parent.MouseButton1Click:connect(function()
if (#game.Teams["Red Team"]:GetPlayers() > #game.Teams["Blue Team"]:GetPlayers()) then
	script.Parent.Text = "Error, teams will result unbalanced"
	wait(2)
	script.Parent.Text = "Join Red"
elseif (#game.Teams["Red Team"]:GetPlayers() <= #game.Teams["Blue Team"]:GetPlayers()) then
	player.TeamColor = BrickColor.new("Bright red")
	torso.CFrame = CFrame.new(322.5, 2.001, 1838)
	script.Parent.Parent.Visible = false
end
end)

I've tried removing the local from player and torso, but it didn't work. Line 2 is the problem and idk how to fix it or even why it errors

1 answer

✓ Accepted answer

The character probably doesn't exist yet when the script initially runs, so you need to wait for it.

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:wait()
local torso = character:WaitForChild("Torso")
-- Other stuff

That should work just fine :)

1 comment

0

Worked like a charm! Thanks!

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