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

Saving String Value Not Working?

game.Players.ChildRemoved:Connect(function(plr)
	dsservice = game:GetService("DataStoreService")
	datastore = dsservice:GetDataStore("Rank")
	datastore:SetAsync(plr.UserId, plr.Rank.Value)
end)

game.Players.ChildAdded:Connect(function()
	dsservice = game:GetService("DataStoreService")
	datastore = dsservice:GetDataStore("Rank")
	local text = datastore:GetAsync("Rank")
	script.Parent.Rank.Value = text
end)

I Am Trying To Save A Rank But After some Tests It Doesn't Save The Rank And I Don't Know Why

2 answers

✓ Accepted answer

Try this

local datastore = game:GetService("DataStoreService")
local rankdatastore = datastore:GetDataStore("Rank")

game.Players.ChildAdded:Connect(function(plr)

	local data
	local success, errormessage = pcall(function()
		data = rankdatastore:GetAsync(plr.UserId.."-rank")
	end)

	if success then
		-- here change the rank value, for example Rank.Value = data
     else
		print("There was an error whilst getting your data")
		warn(errormessage)
    end

end)

game.Players.ChildRemoved:Connect(function(plr)
	local success, errormessage = pcall(function()
		rankdatastore:SetAsync(plr.UserId.."-rank", plr.Rank.Value)
	end)
	if success then
		print("Rank successfully saved")
	else
		print("There was an error saving data")
		warn(errormessage)
	end
end)

it isnt working because you’re not specifying what youre getting, you are just getting ‘Rank’ but you save a value

GetAsync cant have 2 args, at least that works for me. the only arg there is the code, in which case is the players userid, what i do is attach a string to the players userid by doing datastore:GetAsync(player.UserId.. ‘-rank’) which will be able to be seperated from different players, and also be able to grab the right value. also you should wrap the :GetAsync() in a pcall function so it doesnt error incase the :GetAsync() fails. if this helped please accept my answer, else i will be happy to help you in the community chat

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