Problem with Script (Players Highlight)

There is the problem with this script, if anyone know how to fix, feel free to share. I just need to understand what is the problem inside this script because i can’t understand how it works or what i did wrong, so there are like 2 people which can pick up one color, it shouldn’t.
Only 1-blue and 1-red. I know that i can be solve by other way, i need easily fix it out, so share with your idea’s guys, I appreciate that.

local redPart = script.Parent
local clickDetector = redPart:WaitForChild("ClickDetector")

clickDetector.MouseClick:Connect(function(player)
	local colorChoice = player:FindFirstChild("ColorChoice")
	if not colorChoice then
		colorChoice = Instance.new("StringValue")
		colorChoice.Name = "ColorChoice"
		colorChoice.Parent = player
	end
	
	if colorChoice.Value ~= "" then
		warn(player.Name .. " have already picked up the color: " .. colorChoice.Value)
		return	
	end
	
	colorChoice.Value = "Red"
	
	local character = player.Character
	if character then
		local highlight = Instance.new("Highlight")
		highlight.Parent = character
		highlight.Adornee = character
		highlight.FillColor = Color3.new(1, 0, 0)
		highlight.OutlineColor = Color3.new(1, 1, 1)
		highlight.DepthMode = Enum.HighlightDepthMode.Occluded
		highlight.Enabled = true
	end
	
--	if colorChoice.Value == "Red" then
--		clickDetector:Destroy()
--	end
end)

Please help

Have you tried using attributes or tags?
That could definitely help.
Or you could just destroy the ClickDetector once the color is picked.

You could easily track if a part has already been clicked using tags for example:

local RedPart = script.Parent
local ClickDetector = RedPart:WaitForChild("ClickDetector")

local TargetColor = Color3.fromHSV(1, 1, 1)

local function CreateHighlight(player: Player)
	local Character = player.Character or player.CharacterAdded:Wait()

	local Highlight = Instance.new("Highlight")
	Highlight.Parent = Character
	Highlight.Adornee = Character
	Highlight.FillColor = TargetColor
end

ClickDetector.MouseClick:Connect(function(player: Player)
	if not RedPart:HasTag("Used") and not player:HasTag("HasColor") then
		RedPart:AddTag("Used")
		player:AddTag("HasColor")
		CreateHighlight(player)
	end
end)

Yes, i was trying use tags, and as i already said that i wanna do it without destroying ClcikDetector, so yep, i tested it out and it kinda works, but i have 2 questions.

  1. Why I don’t see tags in the part or in player when I’m testing, if as i understand we added 2 tags, 1 to player and 1 to part.
  2. Why my code doesn’t work properly? What I missed or didn’t add. Also what this part is doint or mean:
local function CreateHighlight(player: Player)
	local Character = player.Character or player.CharacterAdded:Wait()

like this → (player: Player)

The (player: Player) tells Luau that the player parameter is a Player instance.
This can be useful because you then get autocomplete for all Player methods and properties.
And the type checker can catch any errors if you try to use something that doesn’t exist on Player.

1 Like

yes, i saw a bit explanation about that, thanks for your answer and solution, thanks again! All the best👍

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.