Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Trying to make a DataStore, but it doesn't appear to save the data. What is wrong with my script?

I have it so that when a player leaves it creates a table with their data, and when they join it retrieves that data, but if they have no data it sets a default value. But it doesn't seem to save the data because when you join, it just sets it to the default value no matter what

script:

 
local Players = game:GetService("Players")

local DataStoreService = game:GetService("DataStoreService")
local PlayerData = DataStoreService:GetDataStore("TestDataV1")


Players.PlayerAdded:Connect(function(player)
	local Data;
	
	local DataFetchSuccess,ErrorMessage = pcall(function()
		Data = PlayerData:GetAsync(player.UserId)
	end)
	
	if not DataFetchSuccess then
		player:Kick("Unable to load your data, please rejoin.")
		return
	end
	
	local Stats = Instance.new("Folder")
	Stats.Name = "Stats"
	
	local Cash = Instance.new("IntValue", Stats)
	Cash.Name = "Cash"
	
	local Stars = Instance.new("IntValue", Stats)
	Stars.Name = "Stars"
	
	Stats.Parent = player

	if Data then
		Cash.Value = Data.Cash
		Stars.Value = Data.Stars
	else
		Cash.Value = 100
		Stars.Value = 5
	end
end)
	

Players.PlayerRemoving:Connect(function(player)
	local Data = {
		
	}
	Data.Cash = player.Stats.Cash.Value
	Data.Stars = player.Stats.Stars.Value
	
	local DataWriteSuccess,ErrorMessage = pcall(function()
		PlayerData:SetAsync(player.UserId, Data)
	end)
	
	if not DataWriteSuccess then
		warn("Couldn't save data of "..player.Name)
	end
end)

1 comment

0

I have the same problem as well. I don't know what's happening.

2 answers

✓ Accepted answer

You need to use game:BindToClose alongside PlayerRemoving. The problem with only having PlayerRemoving is that the server already shuts down whilst your data is still in the middle of saving. BindToClose will prevent this because the function must have run before the game server shuts down.

3 comments

0

This still doesn't help. Even though I'm not the original person who answered the question, I tried this option and it still won't make it work.

0

It actually worked for me, post your script

0

I actually can't because this is a separate question. I might post mine later.

You forgot to use DataStore:SetAsync if the player is new, so there is no data made. Instead, you should try this:

local Players = game:GetService("Players")

local DataStoreService = game:GetService("DataStoreService")
local PlayerData = DataStoreService:GetDataStore("TestDataV1")


Players.PlayerAdded:Connect(function(player)
    local Data;

    local DataFetchSuccess,ErrorMessage = pcall(function()
        Data = PlayerData:GetAsync(player.UserId)
    end)

    if not DataFetchSuccess then
        player:Kick("Unable to load your data, please rejoin.")
        return
    end

    local Stats = Instance.new("Folder")
    Stats.Name = "leaderstats"

    local Cash = Instance.new("IntValue", Stats)
    Cash.Name = "Cash"

    local Stars = Instance.new("IntValue", Stats)
    Stars.Name = "Stars"

    Stats.Parent = player

    if Data then
        Cash.Value = Data.Cash
        Stars.Value = Data.Stars
	else -- Data doesn't exist
		local DefaultValues = {
		cash = 100,
        stars = 5
		}
		PlayerData:SetAsync(player.UserId, DefaultValues) -- set data store to default values
		Cash.Value = DefaultValues.cash -- set leaderstats to default values
		Stars.Value = DefaultValues.stars
    end
end)


Players.PlayerRemoving:Connect(function(player)
    local Data = {

    }
    Data.Cash = player.leaderstats.Cash.Value
    Data.Stars = player.leaderstats.Stars.Value

    local DataWriteSuccess,ErrorMessage = pcall(function()
        PlayerData:SetAsync(player.UserId, Data)
    end)

    if not DataWriteSuccess then
        warn("Couldn't save data of "..player.Name)
    end
end)

I highly recommend the "Datastore Editor" plugin by Crazyman32, it really helped me do this.

This discussion is preserved as it was. New answers, comments, and votes are closed.