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 points save through datastore but when it saves it changes everyones points?

above basicly tells you whats going on heres my script...

-- made by popthecorn145
database = game:GetService("DataStoreService")
game.Players.PlayerAdded:Connect(function(p)
	repeat wait() until p and p.Character -- repeating wait until player and character loads in 
	
	print(p) -- prints the player 
	points = Instance.new("IntValue") -- makes a new int value called points 
	points.Name = "Points"
	points.Parent = p
	points.Value = database:GetDataStore("playerpoints"):GetAsync(p) or 0 -- sets the points value to what is saved or 0 
	p.PlayerGui.PointsGui.TextLabel.Text = points.Value
	
	
	points.Changed:Connect(function()
		
		database:GetDataStore("playerpoints"):SetAsync(p,points.Value) -- on points changed it sets the database to the new amount of points
		p.PlayerGui.PointsGui.TextLabel.Text = points.Value
	
		
	end)
	
	
	
end)

1 answer

✓ Accepted answer

There isn't a very easy, nice and short way of doing this. This is what I came up with, untested, play with it until you get what you're looking for. This uses a RemoteEvent in Workspace, named PointsEvent, for compatibility with experimental mode/FilteringEnabled.

Server side (Script in ServerScriptService)

local pointstore = game:GetService("DataStoreService"):GetDataStore("playerpoints");

game.Players.PlayerAdded:connect(function(player)
	print(player.Name);
	points = Instance.new("IntValue", player);
	points.Name = "Points";
	-- set value equal to the player's entry in pointstore, or 0 if nil (player not in pointstore)
	points.Value = (pointstore:GetASync(player.userId) or 0);
end)

game.Workspace.PointsEvent.OnServerEvent:connect(function(player)
	pointstore:SetASync(player.userId, player.Points.Value);
end)

Client side (LocalScript in StarterPlayerScripts)

local player = game.Players.LocalPlayer;

player:WaitForChild("Points", 10);
player.PlayerGui.PointsGui.TextLabel.Text = tostring(newvalue);

player.Points.Changed:connect(function(newvalue)
	player.PlayerGui.PointsGui.TextLabel.Text = tostring(newvalue);
	game.Workspace.PointsEvent.FireServer();
end)

http://wiki.roblox.com/index.php?title=API:Class/GlobalDataStore

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