I am trying to make a part that gives leaderstats for every second you on the part and I am having trouble with it.
I was think something like this:
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 = "Survival Time" Time1.Value = 0 Time1.Parent = Stat Stat.Parent = Player end function onTouched(part) if part.Parent:findFirstChild("Humanoid")~=nil then local x = 1 local incCur = 1 while wait(x) do for i,plr in pairs(game.Players:GetChildren()) do local ls = plr:FindFirstChild("leaderstats") if ls then local Time = ls:FindFirstChild(Time1) if Time then Time1.Value += 1 + incCur end end end end end script.Parent.Touched:connect(onTouched) end end)
But when I tested it the script didn't work. I need help. Please!
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:
-- Put this code in serverscript service inside a script! 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) -- put this part of the code inside the part you are detecting collisions for, inside a script. 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!