I’ve made a like system for my game, I just had help getting it to work correctly. And now I already have a new issue.
The Like system is a button, if a player clicks the button it adds a like, if they click again it takes away the like. Every player can add a like so it accumulates.
Now the issue is, in my game, there is a like button at every booth. I have a max of 13 people per server. So I will have 13 like buttons that should all work the same.
However, as soon as I duplicate it even once, the script breaks and gets confused with the parts. Since the parts are the same, the script will randomly choose a part and work with that one part only.
you press a button on one of them, the count goes up on the other board, and the other boards button is completely dysfunctional.
I looked into CollectionService and tried using tags and moved the script into the ServerScriptService. I tried following Collective Service tutorials and put the code in the same way.
local CollectionService = game:GetService("CollectionService")
local LikeSystem = CollectionService:GetTagged("LikeSystem")
for i, Like in pairs(LikeSystem) do
local Billboard = game.Workspace.Like:WaitForChild("Board")
local Button = game.Workspace.Like:WaitForChild("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.
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)
end
However it didn’t work. and still does the same thing.
I also have a local script and a remote event thats part of the LikeSystem.
Like = game.Workspace.Like
Button = Like:WaitForChild("Button")
click = Button:WaitForChild("ClickDetector")
Number = 0
Billboard = Like:WaitForChild("Board")
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()
game.ReplicatedStorage.LikeEvent:FireServer(hasLiked) -- Send whether it's a like or unlike
end
click.MouseClick:connect(onClicked)
I’m new to scripting and I’m trying to learn but tutorials cover very basic stuff so I’m always stumped when I’m in situations where I can’t find anything that tackles my problem.
How can I make it where I can have multiple buttons without the script getting confused?