How do i fix this script? When I leave and rejoin the data doesn't save.
local DataService = game:GetService("DataStoreService")
local DataStore = DataService:GetDataStore("DataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder",player)
leaderstats.Name = "leaderstats"
local Credits = Instance.new("IntValue",leaderstats)
Credits.Name = "Credits"
local PlayerUserId = "Player"..player.UserId
Credits.Value = DataStore:GetAsync(PlayerUserId)
end)
game.Players.PlayerRemoving:Connect(function(player)
local PlayerUserId = "Player"..player.UserId
local epics = player.leaderstats.Credits
DataStore:SetAsync(PlayerUserId,epics.Value)
end)

3 comments
Ah thanks, I'm pretty sure it works.
The only thing is that it only works when I go into server mode and change the stats there. When I use a clickdetector it doesn't save the data
Did you play and see if the datastore worked in studio or in the main game as it only works when you're in main game and not playing in studio.