TP error occurs

  1. What do you want to achieve? Keep it simple and clear!
    To see what cause the error in the output.

  2. What is the issue? Include screenshots / videos if possible!
    So I made that when a player touch a part, they will be teleported to their last completed stage and it worked! But an error occurs and I don’t know why.

Below is the script that I use in the part.

script.Parent.Touched:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	local RandomStats = plr.RandomStats 
	local Stage = RandomStats.Stage
	if humanoid ~= nil then
		hit.Parent.HumanoidRootPart.CFrame = CFrame.new(workspace.Stages[Stage.Value].Position + Vector3.new(0,5,0))
	end
end)

The error that occur in the output is ==> Workspace.TPparts.Part.Script:4: attempt to index nil with ‘RandomStats’

Is RandomStats in StarterPlayerScripts before you run the game?

RandomStats is their leaderstats. It’s not in StarterPlayerScripts.

Why don’t you use :SetPrimaryPartCFrame()???

When teleporting the player.

u have to check if the hitpart actually is inside a player character

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
    if plr then
	local RandomStats = plr.RandomStats 
	local Stage = RandomStats.Stage
	if humanoid ~= nil then
		hit.Parent.HumanoidRootPart.CFrame = CFrame.new(workspace.Stages[Stage.Value].Position + Vector3.new(0,5,0))
	end
    end
    end
end)

Ah okay. Try: changing:

local RandomStats = plr.RandomStats 

to:

local RandomStats = plr:FindFirstChild('RandomStats')

reason is that if the hitpart isnt player character it errors so this should check it first

Hello, Appe!
Yes, that true. I forgot to do so. Thanks for your input!

2 Likes