Saves not working

I have saves but the bool values cause a warning “Unable to cast value to object”

Thanks for any help

Code-

local DataStoreService = game:GetService("DataStoreService")
local Store = DataStoreService:GetDataStore("Storage")

game.Players.PlayerRemoving:Connect(function(player)
	local playerUserID = player.UserId
	local Coins = player.Leaderstats.Coins.Value
	--bools
	local Future = player.Leaderstats.Swords.FutureSword.Value
	local Light = player.Leaderstats.Swords.LightSword.Value
	local Dark = player.Leaderstats.Swords.DarkSword.Value
	local Demon = player.Leaderstats.Swords.DemonSword.Value
	local Doom = player.Leaderstats.Swords.DoomSword.Value
	--end bools
	local setSuccess, errorMessage = pcall(function()
		Store:SetAsync(playerUserID, Coins, Future, Light, Dark, Demon, Doom)
	end)
	if not setSuccess then
		warn(errorMessage)
	end
end)

game.Players.PlayerAdded:Connect(function(player)
	local playerUserID = player.UserId
	local getSuccess, Coins, Future, Light, Dark, Demon, Doom = pcall(function()
		return Store:GetAsync(playerUserID)
	end)
	if getSuccess then
		print(Coins)
		print(Future)
		print(Light)
		print(Dark)
		print(Demon)
		print(Doom)
		wait(1)
		player.Leaderstats.Coins.Value = Coins
		player.Leaderstats.Swords.FutureSword.Value = Future
		player.Leaderstats.Swords.LightSword.Value = Light
		player.Leaderstats.Swords.DarkSword.Value = Dark
		player.Leaderstats.Swords.DemonSword.Value = Demon
		player.Leaderstats.Swords.DoomSword.Value = Doom
	end
end)
1 Like

Any help is appreciated as i have never had this problem

-- Saving
Store:SetAsync(playerUserID, Coins, Future, Light, Dark, Demon, Doom)

-- Loading
local getSuccess, Coins, Future, Light, Dark, Demon, Doom = pcall(function()
    return Store:GetAsync(playerUserID)
end)

This is not how these function work. Take a look at DataStore | Documentation - Roblox Creator Hub to see what parameters SetAsync takes and what GetAsync returns.

Assuming those variables are serializable to the data store, you’d probably want to do something like the following:

Store:SetAsync(playerUserID, {
    Coins = Coins,
    Future = Future,
    Light = LIght,
    Dark = Dark,
    Demon = Demon,
    Doom = Doom
})

Then you’d have to adjust your loading behavior too to extract the data from the returned table.

1 Like

How would i do the loading behavior?

Well GetAsync would be returning a table so you’d need to pull the values from the table like:

local getSuccess, Result = pcall(function()
	return Store:GetAsync(playerUserID)
end)
 
if getSuccess then
    local Coins = Result.Coins
    local Future = Result.Future
    -- Continue this for the other values
end

Or instead of pulling out each variable, you can just use the table directly:

player.Leaderstats.Coins.Value = Result.Coins
player.Leaderstats.Swords.FutureSword.Value = Result.Future
-- Continue for the other vlaues

It’s up to you

2 Likes

So this?

local DataStoreService = game:GetService("DataStoreService")
local Store = DataStoreService:GetDataStore("Storage")

game.Players.PlayerRemoving:Connect(function(player)
	local playerUserID = player.UserId
	local Coins = player.Leaderstats.Coins.Value
	--bools
	local Future = player.Leaderstats.Swords.FutureSword.Value
	local Light = player.Leaderstats.Swords.LightSword.Value
	local Dark = player.Leaderstats.Swords.DarkSword.Value
	local Demon = player.Leaderstats.Swords.DemonSword.Value
	local Doom = player.Leaderstats.Swords.DoomSword.Value
	--end bools
	local setSuccess, errorMessage = pcall(function()
		Store:SetAsync(playerUserID, {
			Coins = Coins,
			Future = Future,
			Light = Light,
			Dark = Dark,
			Demon = Demon,
			Doom = Doom
		})
	end)
	if not setSuccess then
		warn(errorMessage)
	end
end)

game.Players.PlayerAdded:Connect(function(player)
	local playerUserID = player.UserId
	local getSuccess, Result = pcall(function()
		return Store:GetAsync(playerUserID)
	end)

	if getSuccess then
		local Coins = Result.Coins
		local Future = Result.Future
		local Light = Result.Light
		local Dark = Result.Dark
		local Demon = Result.Demon
		local Doom = Result.Doom
	end
end)

Yea looks fine to me. Test it and see if it works. Just remember to set the leaderstats values after loading the values.

Also, I’d recommend reading up on the data store documentation just to get a better feel for the API and what’s possible.

1 Like

Works perfect. I’ll read up on it. thank you

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.