I am currently working on a feature where you can click on certain objects and interact with them in different ways (this is why I have many variables set up, and are not being used). I have first started making interaction with an enemy possible (through setting a combat variable to true and making selection box to red, though I hope to expand it later). When testing this script, scrolling over an NPC will not allow you to see a selectionbox, and clicking it will do nothing, it also shows no errors. I would GREATLY appreciate it if you could figure out what is wrong with the following code:
-- Setting all variables local player = game.Players.LocalPlayer local character = player.Character if not character or character.Parent == nil then character = player.CharacterAdded:wait() end local charpos = character.Torso.Position local mouse = player:GetMouse() local selection = Instance.new("SelectionBox", player.PlayerGui) selection.Color = BrickColor.new("Medium stone grey") local combat = script.combat local talking = script.talking local reading = script.reading local questing = script.questing --check target and make variables when mouse moves mouse.Move:connect(function() local target = mouse.Target local hum = target.Parent:FindFirstChild("Humanoid") local hrp = target.Parent:FindFirstChild("HumanoidRootPart") local enemy = target.Parent:FindFirstChild("enemy") local friend = target.Parent:FindFirstChild("friendly") local quest = target.Parent:FindFirstChild("quest") local book = target.Parent:FindFirstChild("book") --check if the target is an npc and not a player if target.Parent:IsA("Model") and hum ~= nil and hrp ~= nil then selection.Adornee = target.Parent -- if the target has the enemy value in it if target.Parent.enemy == true then --and the player clicks on target mouse.Button1Down:connect(function() --and the player is close enough local targetpos = target.Parent.Torso.Position if player:DistanceFromCharacter(targetpos) < 40 then --set combat equal to true, and make the selectionbox red combat.Value = true selection.Ardornee = target.Parent selection.Color = BrickColor.new("Bright red") end end) end end end)
Thank you for reading :)
I would suggest making your anonymous function a normal one, and connecting it to mouse.Move AND mouse.Idle
That way it will fire when the mouse is not actively being moved, but the screen viewpoint is.(IE Scrolling, turning the camera, walking, anything where you don't/don't have to move the mouse
I find that if I try to use game.Players.LocalPlayer
too quickly it will not work.
I do something like this when I try to get the Local Player
repeat wait() until game:GetService("Players") repeat wait() until game:GetService("Players").LocalPlayer local plr = game:GetService("Players").LocalPlayer