How to make datastore script work

Hello, I want to make a script save data of a Boolean value but don’t know how to do that what I done Is followed AlvinBlox’s datastore tutorial and it worked for other things regarding values in the player not in the object itself.

 

game.Players.PlayerAdded:Connect(function(player)
    local Own = script.Parent.Own

	local data
	local success, errormessage = pcall(function()
		data = WeaponData:GetAsync(player.UserId.."-Own")
	end)
	if success then
		Own.Value = data
	else
		print("Error in storage detected")
		warn(errormessage)
	end
end)

 

game.Players.PlayerRemoving:Connect(function(player)
local Own = script.Parent.Own

   local success, errormessage = pcall(function()
		WeaponData:SetAsync(player.UserId.."-Own",script.Parent.Own.Value)
	end)
	
	if success then
		print("Player data successfuly stored")
	else
		print("Failed")
		warn(errormessage)
	end
end)

image

1 Like
data = WeaponData:GetAsync(player.UserId..Own.Value)

This should not be a string?

I copied what the code was on his video

The video might be outdated, i recommend you still test this if you can.

It works on data which is stored in the player and number values

Have you checked your output log? Also make sure API requests are Enabled in Game Settings.

Yes all of that is on and no errors

Please check where the script is located

Its the save script which is on the screenshot

No I mean like in which directory the script is located, Example: Workspace, StarterGUI, ServerScriptStorage

Whole thing is located in the startergui

That’s the problem, Datastores only work on the server, consider moving it to the ServerScriptService instead. which means you’ll have to reference Own differently using the player argument.(passed trough the playeradded and removing events)

2 Likes

The Server Scripts will run in Workspace or ServerScriptService only
and local scripts run if they are a descendant of

  • A Player’s Backpack
  • A Player’s character model
  • A Player’s PlayerGui
  • A Player’s PlayerScripts .
  • The ReplicatedFirst service

I think this is better way to make datastore,
Documentation - Roblox Creator Hub
Actually, you can add more values to one datastore, but that’s just my opinion.

No, you can use a Player.UserId as a key, key doesn’t matters, if it can be accessed from serversided script.

I never asked to change the key, the problem that i thought is that the value is set as a string saved.