Leaderstats only saving 50% of the time?

When I test my game in studio and change my stats, to see if certain aspects of the game are working, they don’t seem to always save when I hit stop. I’m not sure what the issue is since it does work about 50% of the time and I verified that it is running all the way through since it prints “data saved”. I haven’t tested the saving mechanism in a real server yet so I’m wondering if this is just a glitch that only occurs in studio. Either way if anyone else has ran into this problem or knows a solution my code is below.

Thanks

game:BindToClose(function()
	local players = Players:GetPlayers()
    for _, player in pairs(players) do
	local Key = player.UserId
	local DataTable = {
    cash = leaderstats.Cash.Value,
    day = leaderstats.Day.Value,
	pineapple = ingredients.Pineapple.Value,
	olive = ingredients.Olive.Value,
	mushroom = ingredients.Mushroom.Value,
	spinach = ingredients.Spinach.Value,
	bananapepper = ingredients.BananaPepper.Value,
	ham = ingredients.Ham.Value,
	chicken = ingredients.Chicken.Value,
	bacon = ingredients.Bacon.Value,
	pepper = ingredients.Pepper.Value
}

	MyDataStore:SetAsync(Key, DataTable)
end

print("Data saved")
end)

Update: When I change the value of Pineapple I get this error in the output. Anyone know what this means?

[ServerScriptService.Main.Leaderboard/Save:142: attempt to index function with ‘Pineapple’]

2 Likes

I would recommend publishing to Roblox and chedk if it works as it should. I’ve been having a few problems the past few weeks with accessing DataStores from studio and it may just be that you are experiencing the same error.

I tried it in a server and it still didn’t save my cash value properly.

Are you getting any other errors other than the one you stated?

No and normally there are no errors at all if I don’t change the values of the ingredients.

I think this should be in studio bugs. Seems more like a bug then a scripting error :confused:

Could you show line 142 that is outputting the error as from that error you stated it appears that you are attempting to compare the value Pineapple with a function

Line 142 is

pineapple = ingredients.Pineapple.Value,

as shown above. I commented it out and then it errored for olive which is the next ingredient. So its definitely something wrong with the ingredients.