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

How do I make the leaderstats go up?

Hey so I have a datastore script which saves the leaderstat, the name of it is a value which is the child of the script, I need the leaderstat to go up by 500 every 60 seconds.

Here is the script:

game.Players.PlayerAdded:connect(function(player)
local DataStore = game:GetService("DataStoreService")

	 local leader = Instance.new("Folder",player)
leader.Name = "leaderstats"

for i,v in pairs(script:GetChildren()) do 
local d = DataStore:GetDataStore(v.Name)
local x = Instance.new("NumberValue",leader)
x.Name = v.Name
x.Value = d:GetAsync(player.UserId) or v.Value



end
end)

game.Players.PlayerRemoving:Connect(function(player)
	for i,v in pairs(script:GetChildren()) do 
		print("Getting")
		local DataStore = game:GetService("DataStoreService")
		local d = DataStore:GetDataStore(v.Name)
		d:SetAsync(player.UserId, player.leaderstats[v.Name].Value)
		print("Saved")
	end
end)

Thanks

2 comments

0

bruh you didn't even try, this isn't a request site

0

I did, I used while true do etc and it didnt work

1 answer

✓ Accepted answer
game.Players.PlayerAdded:connect(function(player)
    local DataStore = game:GetService("DataStoreService")

    local leader = Instance.new("Folder", player)
    leader.Name = "leaderstats"

    for i, v in pairs(script:GetChildren()) do
        local d = DataStore:GetDataStore(v.Name)
        local x = Instance.new("NumberValue", leader)
        x.Name = v.Name
        x.Value = d:GetAsync(player.UserId) or v.Value

	coroutine.wrap(function() -- Make a new thread so the script wont hang itself and stop on the first while loop, also to migitate wait pausing everything in the current thread
		while x.Parent ~= nil do -- This will check if the value still exists so it will stop if the player leaves
			x.Value = x.Value + 500 -- Increase the value by 500
			wait(60) -- wait 60 seconds
		end
	end)()
    end
end)

game.Players.PlayerRemoving:Connect( function(player)
    for i, v in pairs(script:GetChildren()) do
        print("Getting")
        local DataStore = game:GetService("DataStoreService")
        local d = DataStore:GetDataStore(v.Name)
        d:SetAsync(player.UserId, player.leaderstats[v.Name].Value)
        print("Saved")
    end
end)

5 comments

1

Small note: instead of "x.Value = x.Value + 500" do "x.Value += 500" using the += operator

0

those are the same, it's just personal preference

0

yes

2

Yes, but the new -= and += operators exist to be used, increases workflow.

1

I agree with @zamd157, think of it as "value = value + number" was deprecated in favor of += or -=.

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