Using Tags and Mouse.Target

I’m trying to better optimize my game by using tags, here’s my old script for reference.

Old Script:

--fyi parts of the code have been snipped out in this post
local UserInputService = game:GetService("UserInputService")
local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()
local WhitelistedTooltipItems = {"Takeaway Lid", "Takeaway Sleeve", "Bowl of Cherries", "Cold Drink Lid"}

if not UserInputService.TouchEnabled then
	Mouse.Move:Connect(function()
		local Target = Mouse.Target
		if Target and Target.Parent then
			if table.find(WhitelistedTooltipItems, Target.Parent.Name) then
				if (Player.Character.UpperTorso.Position - Target.Parent.TextHitbox.Position).Magnitude < ItemMagnitude and (Head.CFrame.p - Camera.CFrame.p).Magnitude < CameraMagnitude then
					Tooltip.Visible = true
				end
       		end
		end
	end)
end

Hopefully you kinda understood the jest of what I was doing there, but I wanted to switch over to using tags, it it possible to do so? I have one tag called “test” so do what you will!

So, you want to switch from ToolTip to CollectionService tags? I think I’m understanding this wrong.

1 Like

Yes I want to switch to CollectionService and not use multiple Model Names and a Table.

In that case you should be using the GetTagged method of collection service.

To switch over your current code, you can just use this list only in pairs and see if it is the target.

I know how to use the method GetTagged. In fact I use it many times through out my game.

I’m not to sure what you mean by that.

Some more explanation is a I have a many model’s in workspace that are tagged with "test" and I don’t want to have to always update the script when I change the models names. So I am using tags! Could you show me an example of what you meant by the quote above though?

I meant that you would have a static list of GetTagged("test") and then you would iterate through it and check each one and determine if it matches what the mouse is pointing at.

Oh, alright. I don’t know how I would set that up though.

Probably something like this:

for _, taggedObject in pairs(CollectionService:GetTagged("test")) do
	if taggedObject == Target.Parent and (Player.Character.UpperTorso.Position - Target.Parent.TextHitbox.Position).Magnitude < ItemMagnitude and (Head.CFrame.p - Camera.CFrame.p).Magnitude < CameraMagnitude then
		Tooltip.Visible = true
	end
end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.