So Hovering over the Character works, but the else statement doesn’t print when the mouse is away from the Character. Screen Shot Below
Can you paste the code?, I know why it doesn’t work, and easier explanation with the code
So the problem with this is when the script check if the mouse hovers over a model or nah
since your mouse does not find the target it skips the whole process of checking the if the player is found or no
if mouse.Target then
local Model = model
if Model then -- if the mouse doesn't find Model, it will skip the player check
local Player --doesn't run this part since it already skipped
if Player then -- the player won't be checked
else --this won't be check
print("2") -- which result in not printing this output
end
end
--so your script will skip the above and start running down here
end
That’s because mouse.Target = nil whenever you point at the sky or something.
You have an if statement right after the input changed event checking for mouse.Target
Instead of using the player class to get the character just use the character model
Here’s the code
--// SERVICES
local UIS = game:GetService("UserInputService")
--// VERIABLES
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
--// MAIN
UIS.InputChanged:Connect(function()
if Mouse.Target then
local Model = Mouse.Target:FindFirstAncestorOfClass("Model")
if Model then
local ThePlayer = game.Players:GetPlayerFromCharacter(Model)
if ThePlayer then
local v = Instance.new("Highlight", ThePlayer.Character)
v.Adornee = ThePlayer.Character
v.FillColor = Color3.fromHex("#303030")
v.FillTransparency = 0.75
else
print("2")
end
end
end
end)
thanks but I just explained the above
So what you want to do if you want to it to print out 2 while checking for the player, is to get rid of the check Model thing
UIS.InputChanged:Connect(function()
if Mouse.Target then
local Model = Mouse.Target:FindFirstAncestorOfClass("Model")
local ThePlayer = game.Players:GetPlayerFromCharacter(Model)
if ThePlayer then
local v = Instance.new("Highlight", ThePlayer.Character)
v.Adornee = ThePlayer.Character
v.FillColor = Color3.fromHex("#303030")
v.FillTransparency = 0.75
else
print("2")
end
end)
or if you want to check the model just in case something goes wrong then
if Model then
if ThePlayer then
local v = Instance.new("Highlight", ThePlayer.Character)
v.Adornee = ThePlayer.Character
v.FillColor = Color3.fromHex("#303030")
v.FillTransparency = 0.75
else
print("2")
end
else
print("Model not found")
end
Now i want to remove the highlights, if the user decides to move their mouse.
But I get an error saying, attempt to index nil with “Character”?.
UIS.InputChanged:Connect(function()
if Mouse.Target then
local Model = Mouse.Target:FindFirstAncestorOfClass("Model")
local ThePlayer = game.Players:GetPlayerFromCharacter(Model)
if ThePlayer then
local v = Instance.new("Highlight", ThePlayer.Character)
v.Adornee = ThePlayer.Character
v.FillColor = Color3.fromHex("#303030")
v.FillTransparency = 0.75
else
for _,v in pairs(ThePlayer.Character:GetDescendants()) do --// attempt to index nil with "Character"
if v:IsA("Highlight") then
v:Destroy()
end
end
end
end
end)
Never mind i fixed the issue, i did this instead haha thanks for the help though!.
UIS.InputChanged:Connect(function()
if Mouse.Target then
local Model = Mouse.Target:FindFirstAncestorOfClass("Model")
local ThePlayer = game.Players:GetPlayerFromCharacter(Model)
if ThePlayer then
Highlight(ThePlayer.Character)
else
for _, Plr in pairs(game.Players:GetPlayers()) do
if Plr.Character then
local c = Plr.Character:FindFirstChildOfClass("Highlight")
if c then
c:Destroy()
end
end
end
end
end
end)
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.