I have this while true do loop which is supposed to add 1 to the seconds’ value per second but I made so it does that every 3 seconds. This is because after a player plays for a longer time the loop starts speeding up and the value goes up in like 5 per second. How do I make it so it stays at 1 per second.
while wait(3) do
local g = true
if g then
Player.leaderstats.Seconds.Value += 1
if ga == true then
break
end
g = false
end
I am not really sure about what it is but you made a small mistake at the line « Player.leaderstats.Seconds.Value += 1». Excepted this little thing, i will let someone else answer i think
No, i want it to stay at 1 per second but for some reason if the player plays for longer time, the seconds increase to like 5 per second. This is why I put it at like 1 per 3 seconds.
Maybe the while wait(3) do part is messing things up. I’ve heard that you should avoid wait at all costs. If this doesn’t work, a Heartbeat function should work (hopefully).
I have tried that but it did not work. Here is the full script:
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local HttpService = game:GetService("HttpService")
local RunService = game:GetService("RunService")
local checkpoints = workspace.Checkpoints
local ga = false
Players.PlayerAdded:Connect(function(Player)
local PlayerDataStore = DataStoreService:GetDataStore("PlayerStore", Player.UserId)
warn("Initializing leaderstats...")
local stats = Instance.new("Folder")
stats.Name = "leaderstats"
stats.Parent = Player
local wipeouts = Instance.new("IntValue")
wipeouts.Name = "Deaths"
wipeouts.Parent = stats
local second = Instance.new("IntValue")
second.Name = "Seconds"
second.Parent = stats
local stage = Instance.new("IntValue")
stage.Name = "Stage"
stage.Parent = stats
print("Completed.")
warn("Initializing leaderstats values...")
local success = PlayerDataStore:GetAsync("success")
local PlayerData = PlayerDataStore:GetAsync("PlayerData")
if success == nil or false then
print("Player is currently nil")
PlayerDataStore:SetAsync("success", true)
PlayerDataStore:SetAsync("PlayerData", HttpService:JSONEncode({deaths = 0, sec = 0, stages = 0}))
Player:Kick("Rejoin, you have no data")
elseif success == true then
local decoded = HttpService:JSONDecode(PlayerData)
-- for k,v in pairs(decoded) do
-- print(k, v, type(v))
-- end
Player.leaderstats.Deaths.Value = decoded.deaths
Player.leaderstats.Seconds.Value = decoded.sec
Player.leaderstats.Stage.Value = decoded.stages
print(success)
print("Completed.")
warn("Continuing with normal RunTime")
Player.CharacterAdded:connect(function(Character)
local humpart = Character.HumanoidRootPart
RunService.Stepped:wait()
humpart.CFrame = checkpoints[stage.Value].CFrame+ Vector3.new(0,math.rad(3.5),0)
local d = true
Character:WaitForChild("Humanoid").Died:Connect(function()
RunService.Stepped:wait()
humpart.CFrame = checkpoints[stage.Value].CFrame + Vector3.new(0,math.rad(3.5),0)
if d then
Player.leaderstats.Deaths.Value += 1
end
end)
while wait(3) do
local g = true
if g then
Player.leaderstats.Seconds.Value += 1
if ga == true then
break
end
g = false
end
end
end)
end
end)
Players.PlayerRemoving:Connect(function(Player)
ga = true
local PlayerDataStore = DataStoreService:GetDataStore("PlayerStore", Player.UserId)
warn(string.format("%s IN QUEUE...", Player.Name:upper()))
local death = Player.leaderstats.Deaths.Value
local secs = Player.leaderstats.Seconds.Value
local stagess = Player.leaderstats.Stage.Value
local data = {deaths = death,sec = secs, stages =stagess }
local encoded = HttpService:JSONEncode(data)
PlayerDataStore:SetAsync("PlayerData", encoded)
print("Done")
end)