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

why i got an error "Attemp to index nil with 'leaderstats'" ?

i try to make a teleport system but i got an error

game.Workspace.Pulau1:WaitForChild("P1")
local Teleport = "P1"
local rc = 2

local debounce = true
function Touch(hit) 
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	local leaderstats = player.leaderstats
		if leaderstats.Kepintaran.Value >= rc then
			if debounce then
				debounce = false
				if script.Parent.Locked == false and script.Parent.Parent.Parent.Pulau1:FindFirstChild(Teleport).Locked == false then 
				script.Parent.Locked = true 
				script.Parent.Parent.Parent.Pulau1:FindFirstChild(Teleport).Locked = true
				local Pos = script.Parent.Parent.Parent.Pulau1:FindFirstChild(Teleport)
				hit.Parent.CFrame = CFrame.new(Vector3.new(Pos.Position)) 
				wait(1) 
				script.Parent.Locked = false 
				script.Parent.Parent.Parent.Pulau1:FindFirstChild(Teleport).Locked = false
				end
			end
		debounce = false
	end
end 

script.Parent.Touched:Connect(Touch)

it says, try to index nil with 'leaderstats'

4 comments

0

Player doesn't exist

0

You have to make sure the colliding part is part of a player character

0

how

0

put this at the top: local player = game:GetService("Players")

1 answer

workspace.Pulau1:WaitForChild("P1") -- Change from game.Workspace to workspace
local Teleport = "P1"
local rc = 2

local debounce = false
function Touch(hit) 
	local player = game:GetService("Players"):FindFirstChild(hit.Parent.Name) -- Change game.Players to game:GetService("Players") and change :GetPlayerFromCharacter to FindFirstChild
	if player then
		local leaderstats = player.leaderstats
		if leaderstats.Kepintaran.Value >= rc then
			if not debounce then
				debounce = true -- fix debounce
				if script.Parent.Locked == false and script.Parent.Parent.Parent.Pulau1:FindFirstChild(Teleport).Locked == false then 
					script.Parent.Locked = true 
					script.Parent.Parent.Parent.Pulau1:FindFirstChild(Teleport).Locked = true
					local Pos = script.Parent.Parent.Parent.Pulau1:FindFirstChild(Teleport)
					hit.Parent.CFrame = CFrame.new(Vector3.new(Pos.Position)) 
					wait(1) 
					script.Parent.Locked = false 
					script.Parent.Parent.Parent.Pulau1:FindFirstChild(Teleport).Locked = false
				end
				debounce = false
			end
		end
	end 
end

script.Parent.Touched:Connect(Touch)

3 comments

0

i got this error CFrame is not a valid member of Model

0

lemme guess line 17 ? hit.Parent.CFrame ? yeah Model of teh cchar doesnt have a cframe, reference its HumanoidRootPart instead

0

so it will be like this? hit.Parent.HumanoidRootPart?

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