Attempt to index nil with "Character"

i tried to made math ui that teleport when there are 2 players and i am see problem in line 2 about the character

here is the code:

   local player = game.Players.LocalPlayer
    local character = player.Character

    script.Parent.Main.JoinMath.MouseButton1Click:Connect(function(AddPlayer)
    	game.Workspace.Values.Players.Value = game.Workspace.Values.Players.Value + 1
    end)

    while wait() do
    	if game.Workspace.Values.Players.Value == 0 then
    		script.Parent.Main.PlayerAmount.Text = "2 Players Needed."
    		game.Workspace.Values.Math.Value = false
    	end
    	
    	if game.Workspace.Values.Players.Value == 1 then
    		script.Parent.Main.PlayerAmount.Text = "1 Players Needed."
    		game.Workspace.Values.Math.Value = false
    	end
    	
    	if game.Workspace.Values.Players.Value == 2 or game.Workspace.Values.Players.Value >= 2 then
    		script.Parent.Main.PlayerAmount.Text = "Starting Math Soon."
    		wait(15)
    		game.Workspace.Values.Math.Value = true
    		if character:FindFirstChild("Humanoid") then
    			character.Head.CFrame = CFrame.new(game.Workspace.Math.Position)
    		end
    	end
    	
    	--[[game.Workspace.Values.Math.Changed:Connect(function()
    		if game.Workspace.Values.Math.Value == false then
    			script.Parent.Main.Visible = true
    		end
    		
    		if game.Workspace.Values.Math.Value == true then
    			script.Parent.Main.Visible = false
    		end
    	end)]]--
    end
12 Likes

It might be worth changing the local character to this

local character = player.Character or player.CharacterAdded:wait()

So if the character hasn’t loaded it will wait until it has loaded therefor not calling a nil value.

12 Likes

Add a characteradded event so that way if there isn’t a character, it’ll wait until there is.

local Character = player.Character or player.CharacterAdded:Wait()

1 Like

i tried but is still same…

2 Likes

Oh… I think it’s because you might be using a serverscript… Make sure it’s in a local script… ;-; Not fully sure though because if it was in a serverscript it would’ve broken on line 1…

7 Likes

I assume this is a normal script - change it to a LocalScript. ServerScripts run on the server machine, not on a specified client, so LocalPlayer doesn’t exist

21 Likes

but then how is will teleport all the players and no single player? and i am want to do that change value also to everyone

4 Likes

You can fire a RemoteEvent for that!

13 Likes