So I Got A Trick Or Treat System But When The Person Gets Candy And Rejoins It Goes Back To The Amount From When you first knock on one door.
What do i do ?
Use DataStoreService to save points, it may be hard first time but easy to understand how. DataStoreService guide
Use DataStoreService to save data when a player leaves the game. You can find the api for it here or a video tutorial here. This data store plugin is also useful if you have the robux (200).
local DSService = game:GetService("DataStoreService")
local CandyDS = DSService:GetOrderedDataStore("CandyDS")
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
local leaderstats = player:WaitForChild("leaderstats")
local candyStat = player:WaitForChild("Candy")
local key = "Candy_"..player.UserId
local res = CandyDS:GetAsync(key)
if type(res) == "number" then
print("Successfully loaded!")
candyStat.Value = res
else
print("No data to load.")
candyStat.Value = 0
end
candyStat.Changed:Connect(function(newVal)
local res = CandyDS:SetAsync(key, candyStat.Value)
if res then
print("Successfully saved!")
else
print("Unsuccessfal save attempt.")
end
end)
end)
Players.PlayerRemoving:Connect(function(player)
local leaderstats = player:WaitForChild("leaderstats")
local candyStat = player:WaitForChild("Candy")
local key = "Candy_"..player.UserId
local res = CandyDS:SetAsync(key, candyStat.Value)
if res then
print("Successfully saved!")
else
print("Unsuccessfal save attempt.")
end
end)
game:BindToClose(function()
for _, player in pairs(Players:GetPlayers()) do
local leaderstats = player:WaitForChild("leaderstats")
local candyStat = player:WaitForChild("Candy")
local key = "Candy_"..player.UserId
local res = CandyDS:SetAsync(key, candyStat.Value)
if res then
print("Successfully saved!")
else
print("Unsuccessfal save attempt.")
end
end
end)