Help with saving values

  1. What do you want to achieve? Keep it simple and clear!
    Hi, i want to save a value located inside of the player character, also if any can help me showing me a better method where should i locate the value or is it good inside of the player character

  2. What is the issue? Include screenshots / videos if possible!
    it doesnt really work since when u rejoin ur value its set to 0

  3. What solutions have you tried so far? Did you look for solutions on the Creator Hub?
    I saarched through the devforum but all of them were how to save leaderstats values and not other values

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

This is the code soo far

local plrs = game:GetService("Players")
local ds = game:GetService("DataStoreService")
local splatsave = ds:GetDataStore("splatsave")

plrs.PlayerAdded:Connect(function(plr)
	local char = plr.Character or plr.CharacterAdded:Wait()
	local splatsvalue = char:WaitForChild("SplatBucks_Value")
	
	local plrid = plr.UserId
	local data
	local succes,fail = pcall(function()
		data = splatsave:GetAsync(plrid)
	end)
	
	if succes then
		splatsvalue.Value = data
	else
		error("User Currency Wasnt Loaded Succesfully")
	end
end)

plrs.PlayerRemoving:Connect(function(plr)
	local char = plr.Character or plr.CharacterAdded:Wait()
	local splatsvalue = char:WaitForChild("SplatBucks_Value")
	
	local plrid = plr.UserId
	
	local succes,fail = pcall(function()
		splatsave:SetAsync(splatsvalue.Value)
	end)
end)

I also get an error if needed, here is it
image

and here is also where the value is located at

You are doing

splatsave:SetAsync(splatsvalue.Value)

you need:

splatsave:SetAsync(plrid,splatsvalue.Value)

Problem: you are setting a value as key and set nothing as value

IMPROVED CODE:

local succes,fail = pcall(splatsave.SetAsync,splatsave,plrid,splatsvalue.Value)

You are checking that your anonymous function succeeded but not if any data was loaded. GetAsync can successfully get nil.

Yes this also.
The quick brown fox j

is this how its supposed to look?

local plrs = game:GetService("Players")
local ds = game:GetService("DataStoreService")
local splatsave = ds:GetDataStore("splatsave")

plrs.PlayerAdded:Connect(function(plr)
	local char = plr.Character or plr.CharacterAdded:Wait()
	local splatsvalue = char:WaitForChild("SplatBucks_Value")
	
	local plrid = plr.UserId
	local data
	local succes,fail = pcall(function()
		data = splatsave:GetAsync(plrid)
	end)
	
	if succes then
		splatsvalue.Value = data
	else
		error("User Currency Wasnt Loaded Succesfully")
	end
end)

plrs.PlayerRemoving:Connect(function(plr)
	local char = plr.Character or plr.CharacterAdded:Wait()
	local splatsvalue = char:WaitForChild("SplatBucks_Value")
	
	local plrid = plr.UserId
	
	local succes,fail = pcall(splatsave.SetAsync,splatsave,plrid,splatsvalue.Value)
		splatsave:SetAsync(plrid,splatsvalue.Value)
end)

Why did you kept that line of code?

You are literally doing the exact same thing here but wrapped in pcall:

Deleted it

local plrs = game:GetService("Players")
local ds = game:GetService("DataStoreService")
local splatsave = ds:GetDataStore("splatsave")

plrs.PlayerAdded:Connect(function(plr)
	local splatsvalue = Instance.new("IntValue",plr)
	splatsvalue.Name = "SplatBucks_Value"
	
	local plrid = plr.UserId
	local data
	local succes,fail = pcall(function()
		data = splatsave:GetAsync(splatsvalue)
	end)
	
	if succes then
		splatsvalue.Value = data
	else
		error("User Currency Wasnt Loaded Succesfully")
	end
end)

plrs.PlayerRemoving:Connect(function(plr)
	local char = plr.Character or plr.CharacterAdded:Wait()
	local splatsvalue = char:WaitForChild("SplatBucks_Value")
	
	local plrid = plr.UserId
	
	local succes,fail = pcall(splatsave.SetAsync,splatsave,plrid,splatsvalue.Value)
end)

still doesnt save and this the only error i get
image

You are stepping on a same rakes once again


You need to use your key of a player.
I have no idea how you did format keys that but i will assume it is:

local succes,fail = pcall(splatsave.GetAsync,splatsave,tostring(plrid))

Please read damn documentation

Update, solved it by myself. Heres what my code looks like

local plrs = game:GetService("Players")
local ds = game:GetService("DataStoreService")
local splatsave = ds:GetDataStore("splat_save")

plrs.PlayerAdded:Connect(function(plr)
	local datakey = plr.UserId.. "_splatsave"
	local splats = Instance.new("IntValue",plr)
	splats.Name = "SplatBucks_Value"

	local success,fail = pcall(function()
		return splatsave:GetAsync(datakey)
	end)
	if success then
		if fail then
			splats.Value = fail or 0
		end
	end
end)

plrs.PlayerRemoving:Connect(function(plr)
	local datakey = plr.UserId.. "_splatsave"
	
	local splats = plr:WaitForChild("SplatBucks_Value")
	splatsave:SetAsync(
		datakey,
		splats.Value
	)
end)