Script gets confused when there's duplicate parts

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?

1 Like

ok ok last time

the local script looks ok but i believe the way u could the server script (inside serverscriptservice) is by putting everything in a local function and attaching it to the models

Server Script:

local CollectionService = game:GetService("CollectionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local function likesystem(model)
   local Billboard = model:WaitForChild("Board")
   local Number = 0
   local playerLikes = {}

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

     ReplicatedStorage.LikeEvent.OnServerEvent:Connect(function(player, hasLiked)
	      if playerLikes[player.UserId] == nil then
		    playerLikes[player.UserId] = false
	    end

	    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

	    updateLikes()
    end)
end

local LikeSystems = CollectionService:GetTagged("LikeSystem")

for i, v in pairs(LikeSystems) do
   likesystem(v)
end

this should work atleast

Hmm it has made it where both systems like counts went up, but one button is still dysfunctional. I need to have it where both of them work separately.

1 Like

if thats the case try using this instead

local CollectionService = game:GetService("CollectionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local LikeEvent = ReplicatedStorage:WaitForChild("LikeEvent")

local function likesystem(model)
    local Button = model:WaitForChild("Button")
    local Click = Button:WaitForChild("ClickDetector")
    local Billboard = model:WaitForChild("Board")

    local Number = 0
    local playerLikes = {}

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

    LikeEvent.OnServerEvent:Connect(function(player, hasLiked)
        if playerLikes[player.UserId] == nil then
            playerLikes[player.UserId] = false
        end

        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

        updateLikes()
    end)
end

local LikeSystems = CollectionService:GetTagged("LikeSystem")

for _, v in pairs(LikeSystems) do
    likesystem(v)
end

and i also put the local script inside a local function so lets see if that also works

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CollectionService = game:GetService("CollectionService")

local LikeEvent = ReplicatedStorage:WaitForChild("LikeEvent")

local function likesystem(model)
    local Button = model:WaitForChild("Button")
    local Click = Button:WaitForChild("ClickDetector")
    local Billboard = model:WaitForChild("Board")

    local hasLiked = false

    local function updateButton()
        Button.BrickColor = hasLiked and BrickColor.new("Bright red") or BrickColor.new("Ghost grey")
    end

    local function onClicked()
        hasLiked = not hasLiked
        updateButton()
        LikeEvent:FireServer(hasLiked)
    end

    Click.MouseClick:Connect(onClicked)
end

local LikeSystems = CollectionService:GetTagged("LikeSystem")

for _, v in pairs(LikeSystems) do
    likesystem(v)
end
1 Like

Super close! The buttons works independently however for the server script it still has it where if I like one, it adds a number to all boards. Should I try a separate tag for the board? or will that not make a difference?

try this in the server script

local CollectionService = game:GetService("CollectionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local LikeEvent = ReplicatedStorage:WaitForChild("LikeEvent")

local function likesystem(model)
    local Billboard = model:WaitForChild("Board")
    local playerLikes = {}

    local function updateLikes()
        Billboard.SurfaceGui.TextLabel.Text = tostring(#playerLikes)
        print("Updated likes for model:", model.Name, "Total likes:", #playerLikes)
    end

    LikeEvent.OnServerEvent:Connect(function(player, hasLiked)
        local userId = player.UserId

        if hasLiked then
            if not playerLikes[userId] then
                playerLikes[userId] = true
                updateLikes()
            end
        else
            if playerLikes[userId] then
                playerLikes[userId] = nil
                updateLikes()
            end
        end
    end)
end

local LikeSystems = CollectionService:GetTagged("LikeSystem")

for _, v in pairs(LikeSystems) do
     likesystem(v)
end
1 Like

No number gets added. I appreciate your help however. It means a lot.

1 Like

Thanks but now i believe this script should work just fine

local CollectionService = game:GetService("CollectionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local LikeEvent = ReplicatedStorage:WaitForChild("LikeEvent")

local function likesystem(model)
     local Billboard = model:WaitForChild("Board")
     local playerLikes = {}

    local function updateLikes()
    local likeCount = 0
        for _, liked in pairs(playerLikes) do
            if liked then
                likeCount = likeCount + 1
           end
        end
        Billboard.SurfaceGui.TextLabel.Text = tostring(likeCount)
    end

    LikeEvent.OnServerEvent:Connect(function(player, hasLiked)
        local userId = player.UserId

        if hasLiked then
            if not playerLikes[userId] then
                playerLikes[userId] = true
            end
        else
            if playerLikes[userId] then
                 playerLikes[userId] = nil
            end
        end

        updateLikes()
         print("Total likes for model:", model.Name, "is now:", #playerLikes)
     end)
 end

local LikeSystems = CollectionService:GetTagged("LikeSystem")

for _, v in pairs(LikeSystems) do
    likesystem(v)
end

Im sorry, it still doesn’t work. Doesn’t add a number

1 Like

my bad i went to sleep but but i made come corrections to my old one, see if that works

local CollectionService = game:GetService("CollectionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local LikeEvent = ReplicatedStorage:WaitForChild("LikeEvent")

local function likesystem(model)
    local Billboard = model:WaitForChild("Board", 1) --- waits 1 second before checking
    if not Billboard then
        warn("Billboard not found in model:", model.Name)
    return
end

local likeCount = 0
Billboard.SurfaceGui.TextLabel.Text = tostring(likeCount)

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

LikeEvent.OnServerEvent:Connect(function(player, hasLiked)
    if hasLiked then
        likeCount = likeCount + 1
    else
        likeCount = math.max(0, likeCount - 1) 
    end

    updateLikes()
        print("Total likes for model:", model.Name, "is now:", likeCount)
    end)
end

local LikeSystems = CollectionService:GetTagged("LikeSystem")

for _, model in ipairs(LikeSystems) do
   likesystem(model)
end
1 Like

It still does the same where both boards add a number. Would you like access to my game so you can mess with the script yourself? I’ll even pay for the help

1 Like

im 100% sure this should work it hink maybe not sure but im pretty sure (if this doesnt work ill go inside your game to mess around with it)

local CollectionService = game:GetService("CollectionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local LikeEvent = ReplicatedStorage:WaitForChild("LikeEvent")

local function likesystem(model)
    local Billboard = model:WaitForChild("Board", 1) --- waits 1 second before checking
    if not Billboard then
        warn("Billboard not found in model:", model.Name)
        return
    end

-- Put likecount inside likesystem local function so every model has its own likecount
    local likeCount = 0
    Billboard.SurfaceGui.TextLabel.Text = tostring(likeCount)

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

    LikeEvent.OnServerEvent:Connect(function(player, hasLiked)
        if hasLiked then
            likeCount = likeCount + 1
        else
            likeCount = math.max(0, likeCount - 1) 
        end

        updateLikes()
        print("Total likes for model:", model.Name, "is now:", likeCount)
    end)
end

local LikeSystems = CollectionService:GetTagged("LikeSystem")

for _, model in ipairs(LikeSystems) do
   likesystem(model)
end
1 Like

Almost every Instance in Roblox has UID when created. You can use that to identify things from others. Store a list of the target UID’s then add custom logic. You can either manually do this but I would not recommend. Like you said tags do not work in your case. This can be incorrect, you must use them correctly or they will not work correctly. Collection Service is a great idea and you should be looking in to that more. As long as you are setting tags to certain things correctly. Collection Service should work in your context.

It doesn’t work unfortunately. Let me add you