Need help with Like System I created

I’m creating this art gallery like game and one feature I’m trying to create is a like system. It’s a button that’s on every players art booth, and when a player likes an artist they click the button in support of them.

I’m still learning how to script, I’m kind of noobish. I been watching lots of tutorials and looking at guides for roblox LUA but unfortunately I can’t find the answer to everything I’m trying to do.

What I’m trying to achieve for my like system is that every player can like, and take back their like. It’s meant where you can accumulate likes. But every player can only add and subtract one number.

I have 2 scripts. A server and a local script. In the local script I have it where only the individual player can see their like. The button shows red for if they already clicked the button, and is greyed out if they haven’t clicked the button.

This is the local script

Like = game.Workspace.LikeSystem
Button = Like:WaitForChild("Button")
click = Button:WaitForChild("ClickDetector")
Number = 0
Billboard = Like:WaitForChild("Board")

function onClicked()

	if Button.BrickColor == BrickColor.new("Ghost grey") then
		Button.BrickColor = BrickColor.new("Bright red")

	else if Button.BrickColor == BrickColor.new("Bright red") then
			Button.BrickColor = BrickColor.new("Ghost grey")

		end
	end
end


Button.ClickDetector.MouseClick:connect(onClicked)

For the server script I have it where players can add and subtract their number. The count is visible to everyone.

Here is the script for that

 local Billboard = script.Parent.Board
 local Button = script.Parent.Button
 local Click = Button.ClickDetector
 local Number = 0 -- Starting number is always 0
 local on = false -- When button is clicked, it will turn on. When clicked again, it will turn off.

Click.MouseClick:Connect(function()
	if on == false then -- When button is clicked it will add +1 to the number and turn on.
		on = true
		Number = Number + 1
		Billboard.SurfaceGui.TextLabel.Text = Number -- Displays the number
	else
		on = false -- when button is clicked again it will subtract 1 number and turn off.
		Number = Number - 1
		Billboard.SurfaceGui.TextLabel.Text = Number
	end
end)

It works like this

Unfortunately, since the server script is applied to everyone, that means it only adds and subtracts by one number, controlled by anyone… I tested it with 2 players, and both players could mess with the button and it only went up by one, and then subtracted when another player pressed the button.

I want to have it where every play can add by one so the likes can accumulate.

Unfortunately I’m struggling to make it work like that.

When I take away the line of code that subtracts the number, if clicked again, then a single player can endlessly click the button and accumulate likes.

Example1
Example2

How can I make it where it’s limited to only one like per player?

Hi! The issue is because it isn’t player-specific, so it is sharing across all players. I would use RemoteEvents for sending the player who clicked the button and handling the action of adding the like or removing on the server.

Client side

-- Your variables--
local hasLiked = false -- Track if the player liked
function updateButton()
	if hasLiked then
		Button.BrickColor = BrickColor.new("Bright red") 
	else
		Button.BrickColor = BrickColor.new("Ghost grey") 
	end
end

function onClicked()
	-- Toggle the like status
	hasLiked = not hasLiked

	updateButton()

	-- Send the like action to the server (Please reference your RemoteEvent!)
	game.ReplicatedStorage.LikeEvent:FireServer(hasLiked) -- Send whether it's a like or unlike
end

click.MouseClick:connect(onClicked)

Server side

local playerLikes = {}

local function updateLikes()
	Billboard.SurfaceGui.TextLabel.Text = tostring(Number)
end

game.ReplicatedStorage.LikeEvent.OnServerEvent:Connect(function(player, hasLiked)
	-- Check if the player has interacted with a button like.
	if playerLikes[player.UserId] == nil then
		playerLikes[player.UserId] = false
	end

	-- If the player is liking for the first time
	if hasLiked and not playerLikes[player.UserId] then
		Number = Number + 1
		playerLikes[player.UserId] = true
	elseif not hasLiked and playerLikes[player.UserId] then
		Number = Number - 1
		playerLikes[player.UserId] = false
	end

	-- Just a link function to update the number, as the variable got changed!
	updateLikes()
end)

Hope this helps!

1 Like

Thank you so much! I was actually just watching a video on remote events right before your solution and figured that was what I needed. Your script adjustment helped me out a lot especially with better understanding how remote events work. Thank you thank you thank you <3