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

How can i datastore an leaderstats stringvalue?

How can i datastore an leaderstats stringvalue? i have a leaderstats script and datastore script they are different scripts leaderstats script:

local function onPlayerJoin(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Clicks = Instance.new("IntValue")
	Clicks.Name = "Clicks"
	Clicks.Value = 0
	Clicks.Parent = leaderstats
	
	local Prestiges = Instance.new("IntValue")
	Prestiges.Name = "Prestiges"
	Prestiges.Value = 0
	Prestiges.Parent = leaderstats
	
	local Gold = Instance.new("IntValue")
	Gold.Name = "Gold"
	Gold.Value = 0
	Gold.Parent = leaderstats
	
	local Rank = Instance.new("StringValue")
	Rank.Name = "Rank"
	Rank.Value = "Noob"
	Rank.Parent = leaderstats
end

game.Players.PlayerAdded:Connect(onPlayerJoin)

datastore script:

local DS = game:GetService("DataStoreService"):GetDataStore("SaveMyData")
game.Players.PlayerAdded:Connect(function(plr)
	wait()
	local plrkey = "id_"..plr.userId
	local savevalue = plr.leaderstats.Clicks
	local savevalue2 = plr.leaderstats.Prestiges
	local savevalue3 = plr.leaderstats.Gold
	
	local GetSaved = DS:GetAsync(plrkey)
	if GetSaved then
		savevalue.Value = GetSaved[1]
		savevalue2.Value = GetSaved[2]
		savevalue3.Value = GetSaved[3]
		print("Datastored")
	else
		local NumbersForSaving = {savevalue.Value, savevalue2.Value, savevalue3.Value}
		DS:GetAsync(plrkey, NumbersForSaving)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	DS:SetAsync("id_"..plr.userId, {plr.leaderstats.Clicks.Value, plr.leaderstats.Prestiges.Value, plr.leaderstats.Gold.Value})
end

1 comment

0

;(

1 answer

✓ Accepted answer

As far as I am aware, you cannot save a string value. What I would suggest is to have the Rank be a IntValue also, then have a separate script that loads something like

local function onPlayerJoin(player)
	    local leaderstats = Instance.new("Folder")
	    leaderstats.Name = "leaderstats"
	    leaderstats.Parent = player
	 
	    local Clicks = Instance.new("IntValue")
	    Clicks.Name = "Clicks"
	    Clicks.Value = 0
	    Clicks.Parent = leaderstats
	 
	    local Prestiges = Instance.new("IntValue")
	    Prestiges.Name = "Prestiges"
	    Prestiges.Value = 0
	    Prestiges.Parent = leaderstats
	 
	    local Gold = Instance.new("IntValue")
	    Gold.Name = "Gold"
	    Gold.Value = 0
	    Gold.Parent = leaderstats
	 
	    local Rank = Instance.new("StringValue")
	    Rank.Name = "Rank"
	    Rank.Value = "Noob"
	    Rank.Parent = leaderstats

            local RankSave = Instance.new("IntValue")
	    RankSave.Name = "RankSave"
	    RankSave.Value = 0
	    RankSave.Parent = leaderstats
end

game.Players.PlayerAdded:Connect(onPlayerJoin)

then save the "RankSave" instead of the "Rank"

then I would use a script that runs in a playerGui (I like to do that because it can make and detect changes to server)

the script would read

wait (2) --gives time for data to load

onchange() --this will run the script once so that it loads up one time without detecting a value change

function onchange()
      local ranksave = script.Parent.Parent.leaderstats.RankSave
      local rank = script.Parent.Parent.leaderstats.Rank
      if ranksave.Value == 0 then
      rank.Value = "Noob"
      
      --Continue else if for all ranks you want to have     

      end
end
script.Parent.Parent.leaderstats.RankSave.Changed:Connect(onchange)

then as the game goes on, change the ranksave instead of the rank and this script can change the rank string value.

I hope this helps and makes sense!

1 comment

0

@DemonsEmperor where do i put the wait(2) script

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