✓ Accepted answer
Ah yes, I have tested your script and finally found the issue. You see you created the leaderboard 'survivaltime' within a function. and you name survivaltime = Time1. However, you use this variable 'Time1' in another function. But functions cannot read from other functions. So i suggest making Time1 a global variable. so instead of making a leaderstats inside a function, you should make it at the start of the script. and then modify it inside the function.
make sure the thing that fires the function goes at the end of the script!!
Heres an example:
local dataStoreService = game:GetService("DataStoreService")
game.Players.PlayerAdded:Connect(function(Player)
local Stat = Instance.new("IntValue")
Stat.Name = "leaderstats"
local Time1 = Instance.new("IntValue")
Time1.Name = "SurvivalTime"
Time1.Value = 0
Time1.Parent = Stat
Stat.Parent = Player
end)
local part = script.Parent
x = 1
local debounce = false
local function onPartTouched(otherpart)
local partParent = otherpart.Parent
local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
if humanoid then
print("humanoid found!")
while wait(1) do
if not debounce then
debounce = true
for i,plr in pairs(game.Players:GetChildren()) do
local ls = plr:FindFirstChild("leaderstats")
if ls then
local Time = ls:FindFirstChild('SurvivalTime')
if Time then
Time.Value = Time.Value + 1
wait(1)
debounce = false
end
end
end
end
end
end
end
part.Touched:Connect(onPartTouched)
This code is guaranteed to work! if you look closely you get the idea that I create Stats and Time at the start of the script and not in a function, that way, it is a variable everyone knows inside the script!
So all in all, a function is like private information that noone but that exact function can see. So if you create a variable inside a function you cannot use it anywhere else, so dont make Stats and Time inside a function, make it outside a function in the script.
Hope this wasn't too confusing!
Any questions? Just ask!
PS - i think you also forgot to put a local in the function onTouched!!, also make sure to put a bracket at the end of 'end' to close the function of PlayerAdded!!
This code has been edited as to make sure it works!
9 comments
what is incCur?
it is a number value to tell the script what to add to the leaderstat value after the initial point is added.
At this point,I would recommend using remote events
Hello! I have finally fixed the script! There were numerous errors and I hope you will discover what you did wrong while comparing both scripts!
I have edited my post below follow the instructions pls
Hi thanks for your comment but I have personally tested the script, are you doing something wrong?
Can you add me to team create?
I can even send you a video to show that its working
No thanks, I am done with this kind of thing and I'd rather move on than stress about it.