I'm trying to create a dialogue script where if I was to position my mouse onto an npc and pressed a certain key it would begin the dialogue. However, after putting some conditions the getMouseTarget function ends up returning nil even though my mouse is on an npc. I'd be happy if someone could show me where I messed up. Any help would be appreciated
local target function getMouseTarget() local mousetarget = mouse.Target if mousetarget and mousetarget:IsA("Model") and mousetarget:FindFirstChild("Humanoid") then -- Checks if the target is an actual humanoid compared to a normal model if game:GetService("Players"):FindFirstChild(mousetarget.Name,true) then -- If the humanoid isn't an actual player target = mousetarget end end end mouse.Move:connect(getMouseTarget) print(target.Name)
Target
property is the BasePart
that is under the mouse icon, nil
if it's pointing to the sky. The expected result for you is to get the player. Since the target will be a body part, like the torso, Target.Parent
will get the character, if it is a character.local target local function getMouseTarget() local mousetarget = mouse.Target if mousetarget and mousetarget.Parent.ClassName == "Model" and mousetarget.Parent:FindFirstChild("Humanoid") then if game:GetService("Players"):FindFirstChild(mousetarget.Parent.Name,true) target = mousetarget end end end mouse.Move:Connect(getMouseTarget) -- :connect is deprecated, switch to :Connect print(target.Name)