Croshair color changes when mouse hover over enemy

I want to know how do i go about making a script that changes color when crosshair hover over an enemy.

Example:
This is the normal crosshair.

Png

When color of the crosshair when it is hovering over an enemy.
crosshairred

How do i got about scripting this in a local script!

1 Like

You could make use of Mouse.Target and check when it changed, then check if the target is a player and an enemy.

Wiki links:
https://developer.roblox.com/en-us/api-reference/property/Mouse/Target
https://developer.roblox.com/en-us/api-reference/function/Instance/GetPropertyChangedSignal

ok i will check that out! thanks for the input!

No problem! If this solved your problem remember to mark it as the solution so others know it’s resolved. Have a great day :slight_smile:

can u show me how its done or provide a sample script of it changing.

Sure, here’s an example that will print out when the mouse’s target changes

local tool = script.Parent
tool.Equipped:Connect(function(mouse)
  mouse:GetPropertyChangedSignal("Target"):Connect(function()
    print(mouse.Target)
  end)
end)

To change the icon you can make use of the mouse’s Icon property

Wiki links:
https://developer.roblox.com/en-us/api-reference/property/Mouse/Icon

ok thanks!

Summary

This text will be hidden

Unfortunately :GetPropertyChangedSignal() does not return an RBXScriptSignal which fires when the mouse’s “Target” property changes, you need to to listen to the mouse’s “.Move” event and determine if the mouse has a target from there instead.

local players = game:GetService("Players")
local player = players.LocalPlayer
local team = player.Team
local mouse = player:GetMouse()

mouse.Move:Connect(function()
	local targetPart = mouse.Target
	if targetPart then
		local targetModel = targetPart:FindFirstAncestorOfClass("Model")
		if targetModel then
			local targetPlayer = players:GetPlayerFromCharacter(targetModel)
			if targetPlayer then
				if player ~= targetPlayer then
					if targetPlayer.Team == team then --Targeted player is on the same team.
						--Make crosshair green.
					elseif targetPlayer.Team ~= team then --Targeted player is on an enemy team.
						--Make the crosshair red.
					end
				end
			end
		end
	end
end)

let me try your script and see if it will work!

it didnt work. Idk if you understand but lemme rephrase it. The crosshair stays default but when it hovers over an enemy player it changes color.

It doesn’t work because you need to finish it, I’ve added comments where you need to add code.

i did that and the script is in a tool to it didnt work!

Works fine for me, want to see a gif?

https://gyazo.com/0194e9e5588eb72a502e27c45bd1cd53

local players = game:GetService("Players")
local player = players.LocalPlayer
local team = player.Team
local mouse = player:GetMouse()

local crosshair = script.Parent

mouse.Move:Connect(function()
	crosshair.Position = UDim2.new(0, mouse.X, 0, mouse.Y)
	local targetPart = mouse.Target
	if targetPart then
		local targetModel = targetPart:FindFirstAncestorOfClass("Model")
		if targetModel then
			local targetPlayer = players:GetPlayerFromCharacter(targetModel)
			if targetPlayer then
				if player ~= targetPlayer then
					if targetPlayer.Team == team then
						crosshair.BackgroundColor3 = Color3.new(0, 1, 0)
					elseif targetPlayer.Team ~= team then
						crosshair.BackgroundColor3 = Color3.new(1, 0, 0)
					end
				else
					crosshair.BackgroundColor3 = Color3.new(1, 1, 1)
				end
			else
				crosshair.BackgroundColor3 = Color3.new(1, 1, 1)
			end
		else
			crosshair.BackgroundColor3 = Color3.new(1, 1, 1)
		end
	else
		crosshair.BackgroundColor3 = Color3.new(1, 1, 1)
	end
end)

Here’s the script I used.

Here’s the model file:
model.rbxm (2.9 KB)

i like it but im talking about mouse icon being changed when mouse is hovered over the enemy player

crosshair.BackgroundColor3 = 

You’d just replace these lines with mouse.Icon =.

local crosshair = script.Parent
And remove this from the top of the script.

ok ill try that in the morning