Hello. I have an energy system in my game. The energy (number value) gets saved whenever the player leaves the game. By default, the value is 100 (full energy). However, if a player joins the game for the first time, the energy value is set to 0 although there is no data stored. I want the players to have 100 energy when they join my game for the first time. When they leave then and rejoin, their energy is going to be set to what it was when the players left. here’s the code:
-- insert value
game.Players.PlayerAdded:Connect(function(plr)
local Energy = Instance.new("NumberValue")
Energy.Parent = plr
Energy.Value = 100
Energy.Name = "Energy"
end)
--datastore stuff
local datastoreservice = game:GetService("DataStoreService")
local saveenergy = datastoreservice:GetDataStore("SaveEnergy")
game.Players.PlayerAdded:Connect(function(player)
local energy = player:WaitForChild("Energy")
local data
local success, errormessage = pcall(function()
data = saveenergy:GetAsync(player.UserId, energy)
end)
if success then
energy.Value = data
else
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
saveenergy:SetAsync(player.UserId, player.Energy.Value)
end)
if success then
else
print("Data failed to save")
warn(errormessage)
end
end)
I tried to print the data but it said 0 instead of nil.
local datastoreservice = game:GetService("DataStoreService")
local saveenergy = datastoreservice:GetDataStore("SaveEnergy")
local playeronserver = 0
local bindableEvent = Instance.new("BindableEvent")
game.Players.PlayerAdded:Connect(function(player)
playeronserver += 0
local Energy = Instance.new("NumberValue")
Energy.Parent = player
Energy.Value = 100
Energy.Name = "Energy"
local data
local success, errormessage = pcall(function()
data = saveenergy:GetAsync(player.UserId)
end)
if success then
if data ~= nil then
Energy.Value = data
elseif data == nil then
Energy.Value = 100
end
else
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
saveenergy:SetAsync(player.UserId, player.Energy.Value)
end)
if success then
playeronserver -= 1
bindableEvent:Fire()
else
print("Data failed to save")
warn(errormessage)
end
end)
game:BindToClose(function()
while playeronserver > 0 do
bindableEvent.Event:Wait()
end
end)
wait(1)
if player.Energy.Value == 0 then
player.Energy.Value = 100
end
And also if you don’t work
you can make this here
local DSS = game:GetService("DataStoreService")
local store = DSS:GetDataStore("Joins")
game.Players.PlayerAdded:Connect(function(plr)
local didjoin = false
local firsttime = false
local success, joined = pcall(function()
return store:GetAsync(plr.UserId)
end)
if success then
if joined = 1 then
didjoin = true
else
store:SetAsync(plr.UserId, 1)
firsttime = true
end
end
if not success then
print("failed")
end
end)
the code is from this page btw
From there you can make something
local datastoreservice = game:GetService("DataStoreService")
local saveenergy = datastoreservice:GetDataStore("SaveEnergy")
local playeronserver = 0
local bindableEvent = Instance.new("BindableEvent")
game.Players.PlayerAdded:Connect(function(player)
playeronserver += 0
local playerid = "Player_123456"..player.UserId
local Energy = Instance.new("IntValue")
Energy.Parent = player
Energy.Value = 100
Energy.Name = "Energy"
local data
local success, errormessage = pcall(function()
data = saveenergy:GetAsync(playerid)
end)
if success then
print(data)
if data ~= nil then --- its will load old player data
Energy.Value = data
elseif data == nil then --- if when player 1st time join
Energy.Value = 100 ---- value is up to you
end
else
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerid = "Player_123456"..player.UserId
local success, errormessage = pcall(function()
saveenergy:SetAsync(playerid, player.Energy.Value)
end)
if success then
playeronserver -= 1
bindableEvent:Fire()
else
print("Data failed to save")
warn(errormessage)
end
end)
game:BindToClose(function()
while playeronserver > 0 do
bindableEvent.Event:Wait()
end
end)