I'm making a food bar in my game but to do I need, if the player has joined the game for the first time their food is set to 100 instead of 0, but if they join for the second time it just datastores normally and saves their current food value. I also want to make a max and min food value so the IntValue cannot go over 100 or below 0.
local datastoreservice = game:GetService("DataStoreService")
local AgeDS = datastoreservice:GetDataStore("Age")
local MinutesDS = datastoreservice:GetDataStore("Minutes")
local FoodDS = datastoreservice:GetDataStore("Minutes")
game.Players.PlayerAdded:Connect(function(plr)
local leaderstatsA = Instance.new("Folder", plr)
leaderstatsA.Name = "leaderstatsA"
local age = Instance.new("IntValue", leaderstatsA)
age.Name = "Age"
local minutes = Instance.new("IntValue", leaderstatsA)
minutes.Name = "Minutes"
local food = Instance.new("IntValue", leaderstatsA)
food.Name = "Food"
local success, errormessage = pcall(function()
AgeDS:GetAsync(plr.UserId)
minutes:GetAsync(plr.UserId)
food:GetAsync(plr.UserId)
end)
end)
game.Players.PlayerRemoving:Connect(function(plr)
local sucess, errormessage = pcall(function()
AgeDS:SetAsync(plr.UserId,plr.leaderstatsA.Age.Value)
MinutesDS:SetAsync(plr.UserId,plr.leaderstatsA.Minutes.Value)
FoodDS:SetAsync(plr.UserId,plr.leaderstatsA.Food.Value)
end)
if sucess then
else
warn(errormessage)
end
end)
Thanks!

4 comments
Im kinda confused; youre working with data stores but you dont know how to make basic If statements and 1-time only scripts?
Oh yea I guess I know how to make the max and min but not the 1 time only scripts
btw it's 1 time for the whole of existence not just for the server
nvm I fixed it