Hi, I'm trying to make a mob/npc/creature/whatever you want to call it targeting system, basically when you click on something that has the target circle as a child, it the circle would become visible and when you click away from the thing or clicked on another thing that had a target circle as a child it would switch to that one.
I've tried to attempt this by myself but after countless tears of rage have been shed I decided that maybe I should stop metaphorically bashing my head against a wall and ask for help.
---This is my attempt---- don't laugh at me...
---------------------------------------------------------------------- local Player = game.Players.LocalPlayer local Mouse = Player:GetMouse() local function target() if Mouse.Target.Parent:FindFirstChild("Target") then Mouse.Target.Parent:FindFirstChild("Target").Transparency = 0 return Mouse.Target.Parent:FindFirstChild("Target") end end local function untarget() local targeted = target() if targeted == nil then Mouse.Target.Parent:FindFirstChild("Target").Transparency = 1 end end Mouse.Button1Down:Connect(untarget) ----------------------------------------------------------------------
Hello, here is a quick example I made for you.
local selected for _,v in pairs(workspace:GetChildren()) do if (v:FindFirstChild("ClickDetector") ~= nil) then --use a folder so you don't need this v.ClickDetector.MouseClick:Connect(function() if selected ~= nil then unselect() end doselect(v) end) end end function unselect() selected.Color = Color3.new(163,162,165) selected = nil end function doselect(part) selected = part selected.Color = Color3.new(0,200,0) end
local selected
you declare the selected
variable, which will be the currently selected entity or whatever
for _,v in pairs(workspace:GetChildren()) do
this is pretty much just a way to loop through the children (not descendants) of workspace, you might change that to a folder which contains your entities. I like to use _, v
, you can think of it as key, value
, but in a loop like this key
is always an index number, so we can ignore that.
You don't necessarily need the next line if you're using a folder and 100% sure you'll only have entities in there.
Moving on.
v.ClickDetector.MouseClick:Connect(function() if selected ~= nil then unselect() end doselect(v) end) ... function unselect() selected.Color = Color3.new(163,162,165) -- replace selected = nil end function doselect(part) selected = part selected.Color = Color3.new(0,200,0) -- replace end
So this is possible because when you call Connect
it doesn't just pause execution until the MouseClick
function is called, so you can actually do this in a loop for multiple instances.
Then, we check if there is a currently selected entity, and if there is, we call unselect
. Then we select the new one.
This could be simplified however I feel like it's easier to understand this way.
Hello,
This is a short explanation; maybe use click Detectors in the parts and when clicked parent the Circle to the part?. Make sure to parent the parts in a folder in Workspace. To Get the circle back from the old part what you can do is wrap the if statement in a pairs loop like below,
Code Example, Parenting Circle to clicked Part.
script.Parent.ClickDetector.MouseButton1Click:Connect(function() for _,Instance in pairs(game.Workspace.Folder:GetChildren) do if Instance:FindFirstChild("Target") ~= nil then Instance.Target.Parent = script.Parent end end end)
I hope your head and the wall you banged your head with recover soon....lol