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 go about using DataStoreService with a leaderstat that already exists?

game.Players.PlayerAdded:Connect(function(player)
    local UserInputService = game:GetService("UserInputService")
    local leaderstats = Instance.new("Folder", player)
    local Respect = Instance.new("IntValue", leaderstats)
    leaderstats.Name = "leaderstats"
    Respect.Name = "Respect"
end)

game.ReplicatedStorage.GiveRespect.OnServerEvent:Connect(function(plr)
    plr.leaderstats.Respect.Value = plr.leaderstats.Respect.Value + 1
end)

I want to turn this into a datastore so it saves. How would I go about doing that

3 comments

0

Is there a reason you aren’t making a leaderboard and a save system in the same script?

0

I wanted to make it in the same script, thats why I posted the script

0

Ah, my bad. I misunderstood the title

1 answer

✓ Accepted answer

You can make a datastore by getting the datastore and using :GetAsync() and :SetAsync() to save the values

local datastoreservice = game:GetService('DataStoreService')
local datastore = datastoreservice:GetDataStore('replace with a name u want')

game.Players.PlayerAdded:Connect(function(player)
    local UserInputService = game:GetService("UserInputService")
    local leaderstats = Instance.new("Folder", player)
    local Respect = Instance.new("IntValue", leaderstats)
    leaderstats.Name = "leaderstats"
    Respect.Name = "Respect"
	local respectValue 
	
	local success, error = pcall(function()
		respectValue = datastore:GetAsync(player.UserId..'Respect', Respect.Value)
	end)
	
	if success then
		Respect.Value = respectValue
	else
		warn(error)
	end
end)

game.ReplicatedStorage.GiveRespect.OnServerEvent:Connect(function(plr)
    plr.leaderstats.Respect.Value = plr.leaderstats.Respect.Value + 1
end)

game.Players.PlayerRemoving:Connect(function(player)
	datastore:SetAsync(player.UserId..'Respect', player.leaderstats.Respect.Value)
end)

1 comment

0

thanks, I can review and learn about datastores.

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