local TeleportService = game:GetService(“TeleportService”)
local function onPartTouch(otherPart)
local player = game.Players:GetPlayerFromCharacter(otherPart.Parent)
local teleportData = {
player.leaderstats.coins
}
if player then
TeleportService:Teleport(6163547369, player, teleportData)
end
end
script.Parent.Touched:Connect(onPartTouch)
Error Message
15:01:03.833 Workspace.PLACE TP.Script:5: attempt to index nil with ‘leaderstats’ - Server - Script:5
15:01:03.833 Stack Begin - Studio
15:01:03.833 Script ‘Workspace.PLACE TP.Script’, Line 5 - function onPartTouch - Studio - Script:5
15:01:03.834 Stack End - Studio
I am using a script so that when a part is touched, the player gets teleported to the next level and keep their coins leaderboard stat. I have changed the code multiple times to fix errors, but now, i have no clue what is wrong.
I have checked
Also, here is the receiving code i am gonna use:
receivng code:
local TeleportService = game:GetService(“TeleportService”)
local teleportData = TeleportService:GetLocalPlayerTeleportData()
if teleportData then
local h = part.Parent:findFirstChild(“Humanoid”)
if (h~=nil) then
local thisplr = game.Players:findFirstChild(h.Parent.Name)
if (thisplr~=nil) then
local stats = thisplr:findFirstChild(“leaderstats”)
if (stats~=nil) then
local score = stats:findFirstChild(“Coins”)
if (score~=nil) then
score.Value = score.Value + teleportData.coins
end)
From the error message it looks like the player variable is nil. Attempt to index nil with leader stats, means that on line 5 when you say player.leaderstats.coins, the game reads it as nil.leaderstats.coins. You should check that the otherPart.Parent is a character. Try this:
local TeleportService = game:GetService(“TeleportService”)
local function onPartTouch(otherPart)
local player = game.Players:GetPlayerFromCharacter(otherPart.Parent)
if player then
local teleportData = {
player.leaderstats.coins
}
TeleportService:Teleport(6163547369, player, teleportData)
end
end
script.Parent.Touched:Connect(onPartTouch)
now it says coins is not a valid member of IntValue “Players.fartgas88_999.leaderstats” - Server - Script:6
15:23:59.127 Stack Begin - Studio
15:23:59.127 Script ‘Workspace.PLACE TP.Script’, Line 6 - function onPartTouch - Studio - Script:6
15:23:59.127 Stack End - Studio
should i redefine player, or replace player.leaderstats.coins with game.Players:GetPlayerFromCharacter(otherPart.Parent).leaderstats.coins
function PlayerEntered(new)
stats = Instance.new(“IntValue”)
stats.Name = “leaderstats”
stats.Parent = new
points = Instance.new(“IntValue”)
points.Name = “Coins”
points.Value = 0
points.Parent = stats
player = Instance.new(“StringValue”)
player.Parent = script
player.Value = new.Name
end
game.Players.ChildAdded:connect(PlayerEntered)
inGame = false
function PlayerEnteredAlt(new)
c = script:GetChildren()
if new.className == “Model” then
if new:FindFirstChild(“Head”) then
for i = 1, #c do
if new.Name == c[i].Value then
inGame = true
end
end
if inGame == false then
stats = Instance.new(“IntValue”)
stats.Name = “leaderstats”
stats.Parent = game.Players:playerFromCharacter(new)
points = Instance.new(“IntValue”)
points.Name = “Points”
points.Value = 0
points.Parent = stats
player = Instance.new(“StringValue”)
player.Parent = script
player.Value = stats.Parent.Name
end
end
inGame = false
end
end
game.Workspace.ChildAdded:connect(PlayerEnteredAlt)
hasStats = false
while true do
wait()
c = game.Players:GetChildren()
for i = 1, #c do
if c[i]:FindFirstChild(“leaderstats”) then
hasStats = true
end
if hasStats == false then
cc = script:GetChildren()
for ii = 1, #cc do
if c[i].Name == cc[ii].Value then
cc[ii]:remove()
end
end
end
end
end
What you have mentioned is not a good way of having cooldowns. What you should be using is os.clock() or os.time(). Yielding is certainly not a good way of having debounces.
local before = os.clock()
local function foo()
if (os.clock() - before < 1) then return end -- make sure the cooldown has passed over 1 second
print("called successfully")
before = os.clock()
end
while game:GetService("RunService").Heartbeat:Wait() do
foo()
end