#DataStore dont work
48 messages · Page 1 of 1 (latest)
local DataStoreService = game:GetService("DataStoreService")
local GoldStore = DataStoreService:GetDataStore("GoldStore")
game.Players.PlayerAdded:Connect(function(Player)
-- Create a NumberValue to store the Gold
local goldValue = Instance.new("NumberValue")
goldValue.Name = "Gold"
goldValue.Parent = Player
-- Retrieve value from DataStore
local success, playerData = pcall(function()
return GoldStore:GetAsync(tostring(Player.UserId))
end)
if success then
if playerData ~= nil then
goldValue.Value = playerData
print("Gold value retrieved from DataStore for " .. Player.Name .. ": " .. playerData)
else
goldValue.Value = 225
print("No data found in DataStore. Default value of 225 used for " .. Player.Name)
end
else
warn("Failed to get data for " .. Player.Name .. ": " .. playerData)
goldValue.Value = 225
end
-- Connect value change to save in DataStore
goldValue.Changed:Connect(function()
local success, errorMessage = pcall(function()
GoldStore:SetAsync(tostring(Player.UserId), goldValue.Value)
end)
if not success then
warn("Failed to save data for " .. Player.Name .. ": " .. errorMessage)
else
print("Gold value saved in DataStore for " .. Player.Name .. ": " .. goldValue.Value)
end
end)
end)
game.Players.PlayerRemoving:Connect(function(Player)
local goldValue = Player:FindFirstChild("Gold")
if goldValue then
local success, errorMessage = pcall(function()
GoldStore:SetAsync(tostring(Player.UserId), goldValue.Value)
end)
if not success then
warn("Failed to save data for " .. Player.Name .. ": " .. errorMessage)
else
print("Gold value saved in DataStore for " .. Player.Name .. ": " .. goldValue.Value)
end
end
end)
When i enter the game it says that i have 225 gold, but in my last play i was at a diferent one, like 125
Is this a game that you want to make?
ye
I don't recommend making a game while your also trying to learn Lua
i recommend to learn everything first and to practice certain aspects
untill you know some things to implement into your game
i have a game planned but im learning some stuff about Lua to get myself ready to make a game i want to make
but also make some games first to understand in the long run
ty, but can u help me with the code?
local DataStoreService = game:GetService("DataStoreService")
local GoldStore = DataStoreService:GetDataStore("GoldStore")
game.Players.PlayerAdded:Connect(function(Player)
-- Create a NumberValue to store the Gold
local goldValue = Instance.new("NumberValue")
goldValue.Name = "Gold"
goldValue.Parent = Player
-- Retrieve value from DataStore
local success, playerData = pcall(function()
return GoldStore:GetAsync(tostring(Player.UserId))
end)
if success then
if playerData ~= nil then
goldValue.Value = playerData
print("Gold value retrieved from DataStore for " .. Player.Name .. ": " .. playerData)
else
goldValue.Value = 225
print("No data found in DataStore. Default value of 225 used for " .. Player.Name)
end
else
warn("Failed to get data for " .. Player.Name .. ": " .. playerData)
goldValue.Value = 225
end
-- Connect value change to save in DataStore
goldValue.Changed:Connect(function()
local success, errorMessage = pcall(function()
GoldStore:SetAsync(tostring(Player.UserId), goldValue.Value)
end)
if not success then
warn("Failed to save data for " .. Player.Name .. ": " .. errorMessage)
else
print("Gold value saved in DataStore for " .. Player.Name .. ": " .. goldValue.Value)
end
end)
end)
game.Players.PlayerRemoving:Connect(function(Player)
local goldValue = Player:FindFirstChild("Gold")
if goldValue then
local success, errorMessage = pcall(function()
GoldStore:SetAsync(tostring(Player.UserId), goldValue.Value)
end)
if not success then
warn("Failed to save data for " .. Player.Name .. ": " .. errorMessage)
else
print("Gold value saved in DataStore for " .. Player.Name .. ": " .. goldValue.Value)
end
end
end)
what's the problem?
the game keeps saying that i have 225 gold
** You are now Level 1! **
the data store dont update
maybe because you set the value equal to 225 gold
the starter value is 225
but when i change this, leave and come back, it goes back to 225
data is saved with 225
but it was suposed to save with another gold value
can you video of the situation?
which gold value are you referring to?
local goldValue = Instance.new("NumberValue")
goldValue.Name = "Gold"
goldValue.Parent = Player
no
so is the gold value where you placed the "225" value?
yes, is the starter value when the player first enter the game
but when i spend the gold, the data stil saves 225
but it is suposed to save the actual gold
you saved the data to set it as 225
if success then
if playerData ~= nil then
goldValue.Value = playerData
print("Gold value retrieved from DataStore for " .. Player.Name .. ": " .. playerData)
else
goldValue.Value = 225
print("No data found in DataStore. Default value of 225 used for " .. Player.Name)
end
ohhh
then after you spent it, it's meant to subtract it right?
if not then i don't possibly know
yes
and save the value 225 - spent value
then whatever script you have that does this, make sure your subtracting the value after someone buy's something
im sure that is subtracting it
** You are now Level 2! **
Started test
Gold value retrieved from DataStore for batata_152: 225
Gold value : 225
Spent gold
Gold Value : 125
Ended test
Gold value saved in DataStore for batata_152: 225
u here? @dry ivy
nice
Mb
I feel like you should figure out why the data isn’t saving
Maybe break the code down to understand why that’s happening