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

Teleport Script Giving me a indexing a nil value error?

So I'm trying to making a teleport upon death script for my friend's TSK group, and I keep getting error
12:09:15.386 - Workspace.Script:4: attempt to index local 'game' (a nil value) and I'm not sure why, please help me fix this.

game:GetService('Players').PlayerAdded:connect(function(player)
	player.CharacterAdded:connect(function(character)
		character:WaitForChild("Humanoid").Died:connect(function()
			game.Players.player.Character.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(0, 500000, 0))
		end)
	end)
end)


Appreciate any help given :)

1 comment

0

game may not be the problem, it may be something else, it happens a lot

2 answers

game:GetService('Players').PlayerAdded:connect(function(player)
    player.CharacterAdded:connect(function(character)
        character:WaitForChild("Humanoid").Died:connect(function()
            character.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(0, 500000, 0))
        end)
    end)
end)

Since you already defined player as the actual player object, there is no need to do "game.Players". But also, since you already defined character, there is no need to do " ".Character".

Hope I helped :D

This is (most likely) because you are attempting to do game.Players.player.Character instead of just player. Also you should be using :WaitForChild() so that errors because of objects not existing are minimized (because in this case, what if your character falls off and their character is destroyed, their humanoidrootpart wouldn't exist). But also use :FindFirstChild() for humanoidrootpart just to make sure that it exists when you set its cframe. Also you already got character so why try to get it again? You could just do character:WaitForChild("HumanoidRootPart").CFrame = CFrame.new(0,500000,0)

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