Need help with transitioning materials when hovered

THIS IS MY FIRST POST
For context: I’ve been working on a complex grand-strategy game for 4 months already and I’ve come across a few roadblocks and annoying bugs of which I have been struggling to stamp out, and this is one of them.

What I wanted to work was that the tagged subdivisions/provinces in the game would be able to actively live-respond to the mouse hover (mouse entered turns the subdivision into a neon material, where as the mouse leaving reverts the subdivision back into plastic), but so far the hovering out is unable to revert the aforementioned material. What am I doing wrong?

[I’m still relatively new to LuaU, just only 7 months in]

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local CollectionService = game:GetService("CollectionService")

local function StartHighlightFunct()
	if not mouse.Target then return end

	if CollectionService:HasTag(mouse.Target, "HighlighterTest") then
		script.Highlight.Adornee = mouse.Target
		script.Highlight.FillColor = mouse.Target.Color
		script.Highlight.OutlineColor = Color3.fromRGB(255,255,255)

		script.Highlight.OutlineTransparency = 0
		script.Highlight.FillTransparency = 0.5
		mouse.Target.Material = Enum.Material.Neon
		return
	elseif CollectionService:HasTag(mouse.Target.Parent, "HighlighterTest") then
		script.Highlight.Adornee = mouse.Target.Parent
		script.Highlight.FillColor = mouse.Target.Parent
		script.Highlight.OutlineColor = Color3.fromRGB(255,255,255)

		script.Highlight.FillTransparency = 0
		script.Highlight.OutlineTransparency = 0
		return

	end
	script.Highlight.Adornee = nil
	script.Highlight.FillTransparency = 1
	script.Highlight.OutlineTransparency = 1
	mouse.Target.Material = Enum.Material.Plastic
	return
end

mouse.Move:Connect(StartHighlightFunct)

mouse.Target.Material = Enum.Material.Plastic only works below mouse.Target.Material = Enum.Material.Neon and above return, but FillTransparency = 1 and OutlineTransparency = 1 (the lines after script.Highlight.Adornee = nil) still work just fine., but if I put mouse.Target.Material = Enum.Material.Plastic under OutlineTransparency = 1 it doesn’t work. Why is this?

(Second screenshot may have display issues, sorry if it does)


1 Like

It looks like only parts with the tag “HighlighterTest” become highlighted when they are hovered over. Parts that are not part of “HighlighterTest” have their material changed to plastic. I don’t see any code that changes it back to plastic when it is not touching the mouse anymore.

I don’t know a lot about the collection service but if there is a function that gets all parts with a specific tag you can set all of the parts with a tag to plastic when the function is fired. (quickly found CollectionService:GetTagged() on the roblox docs)

Code: (might not work)

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local CollectionService = game:GetService("CollectionService")

local function StartHighlightFunct()
	if not mouse.Target then return end
	
	local highlighterTestParts = CollectionService:GetTagged("HighlighterTest")

	for _, part in pairs(highlighterTestParts) do
		script.Highlight.Adornee = nil
		script.Highlight.FillTransparency = 1
		script.Highlight.OutlineTransparency = 1
		mouse.Target.Material = Enum.Material.Plastic
	end

	if CollectionService:HasTag(mouse.Target, "HighlighterTest") then
		script.Highlight.Adornee = mouse.Target
		script.Highlight.FillColor = mouse.Target.Color
		script.Highlight.OutlineColor = Color3.fromRGB(255,255,255)

		script.Highlight.OutlineTransparency = 0
		script.Highlight.FillTransparency = 0.5
		mouse.Target.Material = Enum.Material.Neon
		return
	elseif CollectionService:HasTag(mouse.Target.Parent, "HighlighterTest") then
		script.Highlight.Adornee = mouse.Target.Parent
		script.Highlight.FillColor = mouse.Target.Parent
		script.Highlight.OutlineColor = Color3.fromRGB(255,255,255)

		script.Highlight.FillTransparency = 0
		script.Highlight.OutlineTransparency = 0
		return

	end
end

mouse.Move:Connect(StartHighlightFunct)
1 Like

Whoops, still doesn’t change the status quo. I still see neon when I hover out.

hmm… What are you using the collection service for in the first place? I thought it was used for running scripts on multiple parts with the same tag.

I thought it was used for running scripts on multiple parts with the same tag.

That’s still the main intention I’m using it for, after all it IS the main purpose of the CollectionService.

Maybe toggle-tagging may work? (tagging with CollectionService:AddTag() and untagging subdivisions with CollectionService:RemoveTag() depending if hovered or not) because I’m starting to get quite annoyed with this bug

Hiii, I’m not sure if this was what you were trying to achieve, but here it is (Pardon me, I used Attributes instead of tags since I’m not familiar with it, but you should be able to just change that.)

local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()

LastOn = nil

function CheckLand()
	
	if not Mouse.Target then -- If cursor is on the sky or nothing
		script.Highlight.Adornee = nil
	elseif Mouse.Target and Mouse.Target:GetAttribute("IsLand") then
		local Land = Mouse.Target
		script.Highlight.Adornee = Land
	elseif Mouse.Target and not Mouse.Target:GetAttribute("IsLand") then
		print(Mouse.Target, Mouse.Target:GetAttribute("IsLand"))
		script.Highlight.Adornee = nil
	end
	
end

Mouse.Move:Connect(CheckLand)

Result:

2 Likes

That’s exactly what I was going to suggest! Tags are not similar to attributes though so changing the script to use the collection service will require complete rescripting.

1 Like

Exactly this but I want to apply this with materials. The highlight works just fine in the screenshots above; maybe its because the subdivs are UnionOperations?

Sorry, I forgot to add that part

local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()

LastOn = nil

function CheckLand()
	
	if not Mouse.Target and LastOn
		or LastOn and Mouse.Target
		and Mouse.Target ~= LastOn
	then
		LastOn.Material = Enum.Material.Plastic
		LastOn = nil
		script.Highlight.Adornee = nil
	end
	
	if Mouse.Target
		and Mouse.Target:GetAttribute("IsLand")
	then
		LastOn = Mouse.Target
		script.Highlight.Adornee = Mouse.Target
		Mouse.Target.Material = Enum.Material.Neon
	end

end

Mouse.Move:Connect(CheckLand)

1 Like

You’re literally the opposite of me lol. I’m good with CollectionService and tags but I suck badly at attributes, and plus did you put the script(s) in StarterPlayerScripts or somewhere else?

(like show screenshots n’ stuff)
[also sorry for the delayed reply my biology exam coming up]

1 Like

It works! Thank you so much. Just had to add a string attribute to the subdivisions silly me lol
Now I’ll just have to modify this script to accommodate with the tags then :3

1 Like

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