Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Character Is Nil?

Asked by 9 years ago

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)
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 ;-;). TheeDeathCaster 2368 — 9y
0
So how can I fix this? SpazzMan502 133 — 9y
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 TheeDeathCaster 2368 — 9y
0
repeat wait() until character end drew1017 330 — 9y
View all comments (2 more)
0
Thank you Alpha! I moved the WaitFoChild Down the script, and it works now! SpazzMan502 133 — 9y
0
No problem, glad to help man! :D TheeDeathCaster 2368 — 9y

1 answer

Log in to vote
0
Answered by 9 years ago

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
Ad

Answer this question