One part being responsive to Collection Service

So, I am trying to make a hold key to interact system, but I am having trouble with collection service, and only one part makes the UI appear.


Video of problem.

I have tried using coroutines, thinking it was some yielding problem, but it doesn’t seem it is a yielding issue.

This is the current code I am working with.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local CollectionService = game:GetService("CollectionService")

local BindInput = require(ReplicatedStorage.Modules.BindInput)
local Settings = require(ReplicatedStorage.Modules.UserSettings)

local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Gui = script.Parent
local Mouse = Player:GetMouse()

RunService.RenderStepped:Connect(function()
	for _,object in ipairs(CollectionService:GetTagged("Hack")) do
		if Mouse.Target == object then
			Gui.Enabled = true
			print(Mouse.Target)
		else
			Gui.Enabled = false
			print("No Target")
		end
	end
end)
2 Likes

I’d firstly change the function from RunService.RenderStepped to UserInputService.InputChanged, nothing is wrong with that though.
I’ve had the same issue back in the day when I was messing around with interactions and CollectionService, and fixed it by doing this:

if CollectionService:HasTag(PlayerMouse.Target:FindFirstAncestorOfClass("Model"), "YOUR TAG") and not Object then 
--You can change the Target type to anything, in my script I was detecting if its parent is a model, you can change it to detect Parts instead.

I made a variable so I know that my current Target is Object, so just make a nil variable outside of your function:

local Object

Then to check if your mouse is still targeting the object you targeted, do this:

elseif (Object and PlayerMouse.Target ~= Object) then

I suggest adding a magnitude check so when you are moving away from it, it wont target anymore, and cancel out, making Object = nil.
Hope this helped.

2 Likes

Since there is more than 1 object, if the last object being checked is not hovered, the GUI will be disabled even if another object is hovered and has enabled the GUI.

Change it to this:

RunService.RenderStepped:Connect(function()
	for _,object in ipairs(CollectionService:GetTagged("Hack")) do
		if Mouse.Target == object then
			Gui.Enabled = true
			print(Mouse.Target)
                        break --[[Don't look at the rest of the objects, 
because a) they might overwrite Gui.Enabled to false, and
b) there's no way they can also be hovered if this object is hovered, since only 1 object can be hovered at a time.]]
		else
			Gui.Enabled = false
			print("No Target")
		end
	end
end)
1 Like

Yep, this should work, but also for @NewNotHere I believe this should be the more efficient method since there is only one universal GUI object. You would only need to check for the current mouse target instead of looping every HackPart.

RunService.RenderStepped:Connect(function()
		if CollectionService:HasTag(Mouse.Target,"Hack") then
			Gui.Enabled = true
			print(Mouse.Target)
		else
			Gui.Enabled = false
			print("No Target")
		end
end)

1 Like