Data store problem

Hello!

I am currently having an issue with my game development. I added datasave for gui, so when you buy something (like a weapon), you can buy it only once because when you rejoin the game the button saves and says that you already have that weapon.

RobloxScreenShot20230120_143848683

Using this script:

local dds = game:GetService(“DataStoreService”)

local ds = dds:GetDataStore(“datastorecallthiswhatever”)

game.Players.PlayerAdded:Connect(function(player)

local saveValue1 = player:WaitForChild("PlayerGui"):WaitForChild("ScreenGui"):WaitForChild("Shop"):WaitForChild("primaspecifica"):WaitForChild("TextButton").bought
print("working")
local data
local success, errormessage = pcall(function()
	data = ds:GetAsync(player.UserId)
end)

if success then
	print("loading")
	saveValue1.Value = data
else
	print("Error whilst trying to save data!")
	warn(errormessage)
end

if saveValue1.Value == 1 then
	print(player.Name,"owns sword")
	saveValue1.Parent.Text = "Owned"
	saveValue1.Parent.BackgroundColor3 = Color3.new(227, 211, 28)
	saveValue1.Parent.Main:Destroy()
	saveValue1.Parent.LocalScript:Destroy()
end

game.Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
		ds:SetAsync(player.UserId, saveValue1.Value)
	end)

	if success then
		print("Saved!")
	else
		print("Error whilst trying to save data!")
		warn(errormessage)
	end
end)

end)

But if you reset or get killed the data save resets and you need to buy the weapon again.

How can I make the data save rest “Owned” even if you reset or get killed?

Can someone with more knoweldge in scripting help me?
Thank you so much for reading!

Also my ds is: Cosmo#7183

Are you sure you are disabling ResetOnSpawn?


Data Stores don’t just reset, that’s not how they function, according to your code it fires when a Player Joins or Leaves,

Taking this out of the equation we can look at other things to find the issue:

  • Is the Data Saving when the Character Spawns?
    Are you saving Data when the Character is Added into the game?

  • Is The Gui Resetting on Death?
    Are you sure you have ResetOnSpawn Disabled?

  • Is Data being Removed somehow?
    Is there something Removing the Data during games?

These are 3 possible Questions that may be the Issue to your Problem. The more Likely one being
Is The Gui Resetting on Death?

1 Like

yes the gui is resetting on death and I don’t want it to reset

The gui is probably detecting if your “Owner” 's Value is true, if its true its gonna keep it owned, or if false its not gonna be owned, so you probably should stop saving the Owner’s value if you want to buy that item more times, If you want to buy it more times you should make a Int Value that saves the amount of swords you bought.

Also if you want to reset “Owned” value when you reset or get killed, you can simply detect if the player 's character died and set the Owned value to false.
(Make it on a server-sided script on ServerScriptService)

I want the player to buy the sword just once

But you just made that and its working, making to buy it only once.

yes but it resets when player dies and I don’t want the gui to reset, I want it to stay on “owned” and the player to not buy it more than one time

Then its probably cause you didn’t disable ResetOnSpawn on your ScreenGui.

If you disable ResetOnSpawn, it will not reset anything you made while you were using the Gui.
It will not reset script’s changes.

3 Likes

Yeah man your right, didn’t know that! Thank you so much

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