You can write your topic however you want, but you need to answer these questions:
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)
What is the issue? Include screenshots / videos if possible!
It won’t save
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)
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.
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
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
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.
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
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)