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

DataStore not Saving Data?

Hello

I wanted to make a DataStore script for my game (Script in ServerScriptService) and I tried using that and it doesn't work, can someone explain me why? The script including 49 lines.

Here is the script:

ocal DataStoreService = game:GetService("DataStoreService")

local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Coins = Instance.new("IntValue")
	Coins.Name = "Coins"
	Coins.Parent = leaderstats
	
	local playerUserId = "Player_"..player.UserId
	
	
	local data
	local success, errormessage = pcall(function()
		 data = myDataStore:GetAsync(playerUserId)
	end)
	
	
	
	if success then
		Coins.Value = data
	end
	
	
	
end)


game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = "Player_"..player.UserId
	
	local data = player.leaderstats.Coins.Value
	
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(playerUserId, data)
	end)
	
	if success then
		print("Data successfully saved!")
	else
		print("There was an error!")
		warn(errormessage)
	end
	
end)

2 comments

0

make sure to but an if data then because it will errrr

0

On the first line it says "ocal" instead of "local" and idk if that is a problem but just revise your script

1 answer

This often happens because API services are not enabled in roblox studio. Click "Game Settings" in the top bar and select "Permissions". There you will see an option to enable roblox API services. Enable the option and test the game again. It will be working fine.

1 comment

0

You're correct, But I already did it and it DIdn't work before you answered, But thanks for trying to help me!

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