Datastore not saving properly!

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to make an integer value that is equal to 1 or 0 to substitute for a boolean (I heard that you can’t stores bools)

  2. What is the issue? Include screenshots / videos if possible!
    It won’t save

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried changing the name of the datastore, editing the code, rejoining until it works etc.

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!


local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("ShovelDataStore")

local function saveData(player) -- The functions that saves data

	local tableToSave = {
		player.OwnsShovel.Value, -- First value from the table
	}

	local success, err = pcall(function()
		dataStore:SetAsync(player.UserId, tableToSave) -- Save the data with the player UserId, and the table we wanna save
	end)

	if success then
		print("Data has been saved!")
	else
		print("Data hasn't been saved!")
		warn(err)		
	end
end

game.Players.PlayerRemoving:Connect(function(player) -- When a player leaves the game
	saveData(player) -- Save the data
end)

game:BindToClose(function() -- When the server shuts down
	for _, player in pairs(game.Players:GetPlayers()) do
		saveData(player) -- Save the data
	end
end)

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

	local OwnsShovel = Instance.new("IntValue")
	OwnsShovel.Name = "OwnsShovel"
	OwnsShovel.Parent = player

	local data
	local success, err = pcall(function()

		data = dataStore:GetAsync(player.UserId)

	end)

	if success and data then
		OwnsShovel.Value = data[1]
	else
		print("The player has no data!") -- The default will be set to 0	
	end

end)




Images below:
image



Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

2 Likes

Any replies will help, thank you in advance :slight_smile:

1 Like

Have you enabled Enable studio access to API services? Does it print out any errors?

1 Like

Yep @gIwwinnq , I have. My datastore also isnt full of requests, it only stores on leave or server shutdown

1 Like

I was thinking maybe it is because I am storing it on the player?

EDIT: nvm you can store leaderstats on the player so this probably wouldn’t be different. Also another script uses this same code but just with another Datastore name

1 Like

i think that you can store( booleans / strings / numbers ) not 100% sure of that
and if you are making this for a “real” game you should add server locking or use a module that does it to prevent players from losing data

2 Likes

It won’t store the number is the problem and in another post people said if you try to store a boolean it replicates to everyone

1 Like

I am unable to replicate the issue on my side. The way it wouldn’t have saved is that somehow the value is being set to 0 another script, I’m assuming? Are you sure that there are other script that manages the OwnShovels value.

I’m pretty sure you can store boolean just as normal. You can look on the document for that.

2 Likes

what do you mean by replicate to everyone?
you can store
– numbers
– strings
– booleans
what u canot store
– instances and their properties
– things like vec3 / vec2 etc
– functions

2 Likes

Yes another script sets it to 1, but nothing sets it to 0.

Here is the other script

script.Parent.MouseButton1Down:Connect(function()
	if game.Players.LocalPlayer.leaderstats.Brix.Value >= 10 and game.Players.LocalPlayer.OwnsShovel.Value ~= 1 then
		game.Players.LocalPlayer.leaderstats.Brix.Value -= 10
		script.Parent.Parent.Parent.Visible = false
		game.Players.LocalPlayer.OwnsShovel.Value = 1
	end
	
end)
1 Like

I mean that if one person has a boolean stored then it is the same for everyone. I will show you the post I found this it

EDIT: Found it!

1 Like

Nevermind, Misread this post. It said that you can’t use it in replicated storage

I will try to use a boolean inside the player

1 Like

Doesn’t work, when I join it is set to true regardless

Add this under the save data function:

print(player.OwnsShovel.Value)

This will show us what value is being passed on.

1 Like

Oooh I think I got it to work. Lemme see

Nevermind. It won’t work, it just passes on true or false seemingly randomly every time I join

EDIT: It saved false
EDIT2: When I join it prints “The player has no data!”

The ONLY reason why it might be false/not saving, is that you are setting it on the client script, therefore it does not replicate to the server.

When you are setting the OwnShovels to true, ensure that you use a RemoteEvent to set the OwnShovels to true on a serverscript so that it saves properly (and also please use checks that the player meets the requirements on the server as you cannot trust the client)

1 Like

I have an idea, what if I just save it under my other datastore? Then I can cross out conflicting names

ok! Lemme try that rq, it probably will work

I set the value on client side but I store the current value when you leave on server side