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

Character Is Nil?

I have a teleportation localscript that teleports the player to a part of the map when a GUI is clicked..

It works perfectly in studio, but in play mode it doesn't work, saying that it's attempting to index a nil value (character)

I don't really know what the problem is, because I can't WaitForChild() the character.

The localscript inside of a gui

player = game.Players.LocalPlayer
character = player.Character
human = character:WaitForChild("Humanoid")
if not character or not character.Parent then
    character = player.CharacterAdded:wait()
end

character = game.Players.LocalPlayer.Character

Sparkles = Instance.new("ParticleEmitter")
script.Parent.MouseButton1Down:connect(function(bloop)
	Sparkles.Rate = 50
	Sparkles.Parent = character.Torso
	wait(2)
	character.Torso.CFrame = CFrame.new(Vector3.new(-14, 9.5, 28.4))
	Sparkles.Enabled = false
	human.WalkSpeed = 25
	script.Parent.Visible = false
	script.Parent.Parent.Spawn.Visible = true
end)

6 comments

0

In a lot (or some cases), the 'Character' property will be 'nil' before the actual Character for the Player has become existant, until then, it will return 'nil' until the Character is existant, then set the Player's Character property to the Character. :P (I didn't notice this until I did some testing with my own codes ;-;).

0

So how can I fix this?

1

Looking at the code, you are calling the 'WaitForChild' method on line 3 'WaitForChild("Humanoid")' on the Player's Character when the Player's Character could be Non-Existant (or nil) at that current time. :P

0

repeat wait() until character end

0

Thank you Alpha! I moved the WaitFoChild Down the script, and it works now!

0

No problem, glad to help man! :D

1 answer

I think you're overcomplicating things, this is how I would do it.

local tppart = game.Workspace.teleport
function onClick()
script.Parent.Parent.Parent.Character.Torso.CFrame = CFrame.new(tppart.Position.x, tppart.Position.y+3, tppart.Position.z)
script.Parent.Visible = false
end
script.Parent.MouseButton1Click(onClick)

--depends on how many parents you have

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