Infinite yield possible for Highlight Gui

The script is suppose to highlight a part when your mouse is over it. Theres 2 parts where this happens and once you highlight your mouse over one part, you can’t do it to the other part.

Its returning with an Infinite yield and when I use workspace rather than WaitForChild it just gives me an error.

The parts are far enough away from each other to where it’ll disappear off the workspace and come back when the player is close enough.

I need some help on how to fix this.

WaitForChild / FindFirstChild dosen’t work

Game:Getservice(“workspace”).ect… Doesn’t work

Game.workspace Doesn’t work

I’ve tried the methods above


image

local players = game:GetService("Players")
local plr = players.LocalPlayer

local runService = game:GetService("RunService")

local Highlight = game.Workspace.PartScart.Highlight

local mouse = plr:GetMouse()


function ishovering(part)
	if mouse.Target == game.Workspace:WaitForChild("PartScart") then
		print("hovering")
Highlight.Enabled = true
		return true
	else
		Highlight.Enabled = false
		return false
	end
	end



runService.Heartbeat:Connect(function()
	local h = ishovering(game.Workspace:WaitForChild("PartScart"))
end)

Script#2

local players = game:GetService("Players")
local plr = players.LocalPlayer

local runService = game:GetService("RunService")

local Highlight = game:GetService("Workspace"):WaitForChild("ScartPart2").Highlight

local mouse = plr:GetMouse()


function ishovering(part)
	if mouse.Target == game:GetService("Workspace"):WaitForChild("ScartPart2") then
		print("hovering")
Highlight.Enabled = true
		return true
	else
		Highlight.Enabled = false 
		return false
	end
	end


runService.Heartbeat:Connect(function()
	local h = ishovering(game:GetService("Workspace"):WaitForChild("ScartPart2"))
end)
1 Like

Your codes is not efficient but your issue might be because the .Heartbeat runs before the part has even loaded.

Here is a quick code i made on mobile, just make sure you use your custom tag name in the code and assign the tag to your highlight holder. Put in StarterPlayer

This code wont make u have to disable streamingmode and is more efficient and made for scalability.

— Service
local RunService = game:GetService(”RunService”)
local Players = game:GetService(”Players”)

— Player
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()

— Temp
local LastTarget

RunService.PreRender:Connect(function()
if Mouse.Target and Mouse.Target:HasTag(”MyHighlightTag” and LastTarget ~= Mouse.Target then
local Highlight = Mouse.Target:FindFirstChildOfClass(”Highlight)

if Highlight then
LastTarget = Mouse.Target
Highlight.Enabled = true
end
else
LastTarget.Highlight.Enabled = false
end
end)
1 Like

Hi, @Nullendex.

This is because you haven’t setted the timer on second argument of WaitForChild(target,wait-time)

make the part persistent in streaming otherwise it will be invisible in outside of streaming area

1 Like

its not the workspace. It just cant find the ScartPart2 in

local Highlight = game:GetService("Workspace"):WaitForChild("ScartPart2").Highlight

Do you have StreamingEnabled on?
If so then you should make sure to put your part into a model and then set the ModelStreamingMode on the model to Persistent so its loaded all the time.

1 Like

I turned them into models and made them persistent, this worked, thank you.

1 Like

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