How to make a Point adding system if I'm using datastore?

  1. I want to make a block add points to a player Cash Instance.

  2. I have no idea how to connect the Point block to the datastore cash Instance to add points.

  3. There were nothing related to what I’m asking for to solve my problem.

local Player = game:GetService("Players")

function addPoint()
	if Player then 
		game:FindFirstChild("leaderstats")
		if game:FindFirstChild("leaderstats")then
			print("Found leaderstats")
		end
	end
end
	RubyPoint.ClickDetector.MouseClick:Connect(addPoint)

If you make a datastore script that saves the points when a player leaves the points will save.

I want the block they click to add points…

leaderstats don’t exist and they are not automatically added into the game. They are rather manually added through a script using the PlayerAdded event, and putting the leaderstats folder into the player that’s passed as a parameter by the event

I have a datastore system with the leader stats already created when player joins the game.

You have to create a points value inside of the leaderstats folder. Once you do that you can just use plr.leaderstats.Points.Value += 1

local dataStore = DataStoreService:GetDataStore("MyDataStore") -- This can be changed to whatever you want

local function saveData(player) -- The functions that saves data

	local tableToSave = {
		player.leaderstats.Money.Value; -- First value from the table
		player.leaderstats.Coins.Value -- Second value from the table
	}

	local success, err = pcall(function()
		dataStore:SetAsync(player.UserId, tableToSave) -- Save the data with the player UserId, and the table we wanna save
	end)

	if success then -- If the data has been saved
		print("Data has been saved!")
	else -- Else if the save failed
		print("Data hasn't been saved!")
		warn(err)		
	end
end


game.Players.PlayerAdded:Connect(function(player) -- When a player joins the game

	-- // Assigning player stats //
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Money = Instance.new("IntValue")
	Money.Name = "Money"
	Money.Parent = leaderstats

	local Coins = Instance.new("IntValue")
	Coins.Name = "Coins"
	Coins.Parent = leaderstats

	local data -- We will define the data here so we can use it later, this data is the table we saved
	local success, err = pcall(function()

		data = dataStore:GetAsync(player.UserId) -- Get the data from the datastore

	end)

	if success then -- If there were no errors and player loaded the data

		Money.Value = data[1] -- Set the money to the first value of the table (data)
		Coins.Value = data[2] -- Set the coins to the second value of the table (data)

	else -- The player didn't load in the data, and probably is a new player
		print("The player has no data!") -- The default will be set to 0
	end

end)

game.Players.PlayerRemoving:Connect(function(player) -- When a player leaves
	local success, err  = pcall(function()
		saveData(player) -- Save the data
	end)

	if success then
		print("Data has been saved")
	else
		print("Data has not been saved!")
	end
end)

game:BindToClose(function() -- When the server shuts down
	for _, player in pairs(game.Players:GetPlayers()) do -- Loop through all the players
		local success, err  = pcall(function()
			saveData(player) -- Save the data
		end)

		if success then
			print("Data has been saved")
		else
			print("Data has not been saved!")
		end
	end
end)

MouseClick already passes the player who clicked it as a parameter. So you can just do some like:

RubyPoint.ClickDetector.MouseClick:Connect(function(Player) -- MouseClick already passes the player who clicks as an argument
	local LeaderStats = Player:FindFirstChild("leaderstats"); -- Look for the leaderstat folder inside player 
	if LeaderStats then -- Check if its found (will be nil if it doesn't exist)
		local Money = LeaderStats:FindFirstChild("Money"); -- Look for money and cash 
		local Cash = LeaderStats:FindFirstChild("Coins");
		if Money and Cash then -- Check if exist 
			print(Player.Name.." has clicked the button!! Giving rewards!!");
			Money.Value += 1; -- Add 1 coin or whatever you want
			Cash.Value += 1;
		end;
	end;	
end);

There may be a few typo’s as I am on mobile atm. I also didn’t check your datastore script so I’m assuming that its creating the folders and everythings working fine. Also if you are working with ClickDetectors, its best that you do a magnitude check to make sure the player is actually near the part he/she is clicking to prevent exploiters from cframing the parts to themselves or just increasing the MaxActivationDistance.

Hopefully this helps and goodluck!

1 Like

Yes the datastore is working fine, your script also worked thanks a lot. Now I have to see why player points not saving.

Is the datastore broken or somthing?? My progress never saves.