Gun Cursor flickering

I have a script that changes the custom cursor color to red if the player hovers over an “enemy”, but it flickers a lot between white and red, what do I do?

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local show = false
local updating = false

game:GetService("RunService").Heartbeat:Connect(function()
     if show == true then
          if updating == false then
               updating = true
               updateCursor(Color3.new(1, 0, 0))
               updating = false
          end
     else
          if updating == false then
               updating = true
               updateCursor(Color3.new(1, 1, 1))
               updating = false
          end     
     end
end)

mouse.Move:Connect(function()
     if mouse.Target then
          if mouse.Target.Parent:FindFirstChild("Humanoid") then
               if not game.Players:FindFirstChild(mouse.Target.Parent) then
                    show = true
               else
                    local p = game.Players:GetPlayerFromCharacter(mouse.Target.Parent)
                    if p.Team then
                         if p.Team ~= player.Team then
                              show = true
                         else
                              if p.Team == player.Team then
                                   show = true
                              end
                         end
                    else
                         show = true
                    end
               end
          else
               show = false
          end
     else
          show = false
     end
end)

function updateCursor(color)
     local c = player.PlayerGui:FindFirstChild("GunGUI")
     if c then
          local m = c:FindFirstChild("Cursor")
          if m then
               for i,v in pairs(m:GetChildren()) do
                    if v:IsA("ImageLabel") then
                         v.BackgroundColor3 = color
                    end
               end
          end
     end
     return true
end

This code is very tough to read due to the unnecessary amounts of boolean values.

This can be greatly shortend by using ternary operators and AND statements like so.

RunService.Heartbeat:Connect(function(dt)
       local target = mouse.Target

       if target ~= nil 
            local player = game.Players:FindFirstChild(target.Parent.Name) 
   
            if player ~= nil and player.Character ~= nil then
                   updateCursor(redColor)
                   return
            end  
      end

     updateCursor(whiteColor)
end)

This is a boiled down and easier to read version of the above code. I wrote it inside the forum so it might have some issues regarding api spelling but it should work logically. Also im assuming the MouseCursor function involved setting the mouse.Icon in the code. If not, you can just make the adjustments.

The thing is that I’m not changing the mouse icon but a custom cursor that has image labels and frames within it that all needs changing.
And yes sorry for the badly written code, I’ve been trying a lot of different solutions and never got to optimizing it.

In that case you can just flip the mouse.Icon with label.Color and make the necessary ternary changes. Let me know if it still has issues.

But I can’t just do that because there are multiple frames that have to be changed which is why I have the loop function.
Please just tell me how to fix my problem instead of cleaning my code.

That is a fix, it should work fine. But you still didnt post the updateCursor code (which is where the issue lies I suspect) so I cant solve it. A

EDIT: ah nevermind. I missed the code. My mistake!

Can you replace your code with mine however? Since i believe the issue lies with the boolean values and just some weird edge case in which my code should solve.

I also edited the posted code above to involve updateCursor as well so you should be able to copy-paste it into your source.

I just rewrote the code and everything works now, thanks for trying to help.

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