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

My game's datastore doesnt save?

I have a datastore, it does print the load portion, but it doesnt print the save portion so I assume it doesnt work. Why is this? I cant seem to find a solution for this. The script also doesn't print any errors whatsoever.

local datastore = game:GetService("DataStoreService")
local mydatastore = datastore:GetDataStore("mydatastore")


game.Players.PlayerAdded:Connect(function(player)
	
	-- DEFINING VALUES --
	local stam = Instance.new("IntValue")
	stam.Value = 100
	stam.Name = "Stamina"
	stam.Parent = player
	
	local maxstam = Instance.new("IntValue")
	maxstam.Value = 100
	maxstam.Name = "MaxStamina"
	maxstam.Parent = player
	
	local coins = Instance.new("IntValue")
	coins.Value = 0
	coins.Name = "coins"
	coins.Parent = player
	
	local fs = Instance.new("StringValue")
	fs.Value = "Punch"
	fs.Name = "FS"
	fs.Parent = player
	
	local f = Instance.new("StringValue")
	f.Value = "Dirt Dirt Fruit"
	f.Name = "F"
	f.Parent = player
	
	local w = Instance.new("StringValue")
	w.Value = "Punch"
	w.Name = "W"
	w.Parent = player
	-----------------------------------------------------
	
	------------- DATA RETRIEVING ------------------
	
	local data
	pcall(function()
		data = mydatastore:GetAsync(player.UserId)
	end)
	
	if data == nil then print("No data")
	else print("Data found")
		coins.Value = data[4]
		fs.Value = data[1]
		f.Value = data[2]
		w.Value = data[3]
	end
	
	-----------------------------------------------------
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	------------- DATA SAVING -----------------------
	
	local success, errormessage = pcall(function()
		mydatastore:SetAsync(player.UserId,{player.FS.Value,player.F.Value,player.W.Value,player.coins.Value})
	end)
	
	if success then
		print("Data saved for "..player.Name)
	else
		print("Data not saved for "..player.Name)
		warn(errormessage)
	end
	
	-----------------------------------------------------
end)

1 answer

Hello. Try using an actual pcall with the success and errorMessage variables on the PlayerAdded event. Also, make sure you have API Services enabled and you're not in-studio.

local datastore = game:GetService("DataStoreService")
local mydatastore = datastore:GetDataStore("mydatastore")


game.Players.PlayerAdded:Connect(function(player)

    -- DEFINING VALUES --
    local stam = Instance.new("IntValue")
    stam.Value = 100
    stam.Name = "Stamina"
    stam.Parent = player

    local maxstam = Instance.new("IntValue")
    maxstam.Value = 100
    maxstam.Name = "MaxStamina"
    maxstam.Parent = player

    local coins = Instance.new("IntValue")
    coins.Value = 0
    coins.Name = "coins"
    coins.Parent = player

    local fs = Instance.new("StringValue")
    fs.Value = "Punch"
    fs.Name = "FS"
    fs.Parent = player

    local f = Instance.new("StringValue")
    f.Value = "Dirt Dirt Fruit"
    f.Name = "F"
    f.Parent = player

    local w = Instance.new("StringValue")
    w.Value = "Punch"
    w.Name = "W"
    w.Parent = player
    -----------------------------------------------------

    ------------- DATA RETRIEVING ------------------

    local data

    local success, errorMessage = pcall(function()
        data = mydatastore:GetAsync(player.UserId)
    end)

	if success then
		if data then
		        coins.Value = data[4]
		        fs.Value = data[1]
		        f.Value = data[2]
		        w.Value = data[3]
		end
	else
		warn(errorMessage)
    end

    -----------------------------------------------------
end)

game.Players.PlayerRemoving:Connect(function(player)

    ------------- DATA SAVING -----------------------

    local success, errormessage = pcall(function()
        mydatastore:SetAsync(player.UserId,{player.FS.Value,player.F.Value,player.W.Value,player.coins.Value})
    end)

    if success then
        print("Data saved for "..player.Name)
    else
        print("Data not saved for "..player.Name)
        warn(errormessage)
    end

    -----------------------------------------------------
end)

1 comment

0

Thanks, I'll try this.

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