How would I make functioning goggles?

The purpose of the goggles is to clone a highlight object into each token in a folder so the player can see them. Sort of like x-ray. It also clones a accessory onto the character. I also am struggling on making it appear in a certain distance so it isnt too OP. So far every attempt I have done has failed and no errors have happened. I am really confused on what to do:

NOTE: Both scripts and the event are in the tool

Client/Local Script:

local Player = game.Players.LocalPlayer

script.Parent.Equipped:Connect(function()
	script.Parent.WearHat:FireServer(Player, "Equip")
end)

script.Parent.Equipped:Connect(function()
	script.Parent.WearHat:FireServer(Player, "Unequip")
end)

Server/Normal Script:

local ISEquipped = false

local Hat = game.ReplicatedStorage:WaitForChild("Goggles_TOOL")

script.Parent.Equipped:Connect(function()
	ISEquipped = true
end)

script.Parent.Unequipped:Connect(function()
	ISEquipped = false
end)

script.Parent.WearHat.OnServerEvent:Connect(function(plr, status)
	if plr.Character:WaitForChild("Humanoid") then

		if status == "Equip" then
			Hat:Clone().Parent = plr.Character
			Hat.Handle.ON:Play()

			for _,v in pairs(workspace.Tokens:GetChildren()) do
				if v:IsA("MeshPart") then
					local Highlight = script.TokenHighlight:Clone()
					Highlight.Parent = v
				end
			end
		elseif status == "Unequip" then

			plr.Character:FindFirstChild("Goggles_TOOL"):Destroy()

			for _,v in pairs(workspace.Tokens:GetChildren()) do
				if v:IsA("MeshPart") then
					v.TokenHighlight:Destroy()
				end
			end

			Hat.Handle.OFF:Play()
		end
	end
end)
1 Like

Okay, from what i understand the only issue you have is finding the distance between two points (the player and the object “v”), you could use Vector Magnitude to calculate the distance between the two points and if that distance is higher than say 30 studs then don’t show the object.

if (plr.Character:WaitForChild("HumanoidRootPart").CFrame.Position - v.CFrame.Position).Magnitude <= 30 then -- get the magnitude between two points
      -- is withing 30 studs of the player
else
      -- is more than 30 studs away from the player
end
1 Like

Hmmm, I see. Im thinking that there are some problems in my script as the goggles don’t appear or the highlights.

Maybe also try setting the Hightlight.Adornee to v, might be the reason why the highlight isn’t showing up. Try debugging if that doesn’t work.

I’ll check all these methods now! Thanks!

It seems to be a bit delayed and doesn’t do it for any tokens except my character… Any ideas?

New code:

local ISEquipped = false

local Hat = game.ReplicatedStorage:WaitForChild("Goggles_TOOL")

local Range = script:GetAttribute("Range")

script.Parent.Equipped:Connect(function()
	ISEquipped = true
end)

script.Parent.Unequipped:Connect(function()
	ISEquipped = false
end)

script.Parent.WearHat.OnServerEvent:Connect(function(plr, status)
	if plr.Character:WaitForChild("Humanoid") then

		if status == "Equip" then
			Hat:Clone().Parent = plr.Character
			Hat.Handle.ON:Play()

			for _,v in pairs(workspace.Tokens:GetChildren()) do
				if (plr.Character:WaitForChild("HumanoidRootPart").CFrame.Position - v.CFrame.Position).Magnitude <= Range then 
					local Highlight = script.TokenHighlight:Clone()
					Highlight.Parent = v
					Highlight.Adornee = v
					Highlight.Enabled = true

				end
			end

		elseif status == "Unequip" then

			plr.Character:FindFirstChild("Goggles_TOOL"):Destroy()

			for _,v in pairs(workspace.Tokens:GetChildren()) do
				v.TokenHighlight:Destroy()
			end

			Hat.Handle.OFF:Play()
		end
	end
end)

You can only have 31 higlights at a time being displayed, which may be a source of your problem. More than this amount causes highlights not to be rendered.

Also, highlights are currently still kinda buggy from my own testing. You might want to try something different, like the older SelectionBox until they improve highlighting.

Thanks for this! I forgot that existed, Would there be a better way to display a x-ray like effect?

If you just want a purely gameplay focus with the xraying ability and don’t care too much about the effect of having the object itself be visible with an outline, you could use ui to denote where objects are to the player, or you could make everything that isn’t the objects transparent.

Yup Highlights are kinda buggy. Just to add, I tested your script and works fine. Im using only 10 tokens so the issue its not the amount (in your script).

Just make sure the Highlight is not enabled until its parented to the Token “v” (the one inside the script thats gonna be cloned). After its parented enable it.
This occurs (very annoying) when a Highlight is created and parented to something, should be disabled. This happens to me when I bring an entire map from ServerStorage and it only contains 1 highlight, but entire map, absolutely all its parts gets the highlight effect, not the instance just the effect (“the lines”)

I tested your script and works fine for me, unless the little editing I made changed any behaviour

I’ve had a idea to use beams that just somewhat route the player to the token. I wonder if that may be better than managing a limit of 31.

Absolutely any other approach you like its better than handling only 31 objects highligted in screen. @MegabyteOfficial already gave some ideas of more friendly approaches.
I heard highlights glitch in mobiles too.

This is what I started with and it seems to not produce any beams. Im wondering if this is a issue with something else now.

local Hat = game.ReplicatedStorage:WaitForChild("Goggles_TOOL")

local Range = script:GetAttribute("Range")

function Guide(token, status, plr)
	if status == "ON" and (plr.Character:WaitForChild("HumanoidRootPart").CFrame.Position - token.CFrame.Position).Magnitude <= Range then
		
		local A0 = Instance.new("Attachment", plr.Character:WaitForChild("HumanoidRootPart"))
		A0.Name = "A0"
		
		local A1 = Instance.new("Attachment", token)
		A1.Name = "A1"
		
		local Beam = Instance.new("Beam")
		Beam.Color = ColorSequence.new(token.Color, token.Color)
		Beam.Parent = token
		
		Beam.Name = "TokenHighlight"
		
	elseif token:FindFirstChild("TokenHighlight") then
		token:FindFirstChild("TokenHighlight"):Destroy()
		token.A1:Destroy()
		plr.Character:WaitForChild("HumanoidRootPart").A0:Destroy()
	end
end


script.Parent.WearHat.OnServerEvent:Connect(function(plr, status)
	if plr.Character:WaitForChild("Humanoid") then
		for i, token in pairs(workspace.Tokens:GetChildren()) do
			Guide(token, status, plr)
		end
	end
end)

Ohh, one more thing I never tried.
Since I experienced a whole map, with thousand of parts all of them gets highlighted by only having 1 instance of highlight, could help to test if you can only use 1 highlight instance to outline ALL tokens at the same time, instead of having 1 per token.

Just place 1 highlight inside the folder that holds all tokens, instead of parenting one per token.
… oh yup… taking in count that you want to read the distance… that would be changing the parent of the tokens between 2 folders… the far tokens and the closest ones… nah… I think thats not the best idea…

Would a issue be that the tokens spawn and all that. Since the tokens are technically coins and all that.

EDIT: I reduced my script into atoms and it seems that its mainly just that it won’t change colors in general…?

local Hat = game.ReplicatedStorage:WaitForChild("Goggles_TOOL")

local Range = script:GetAttribute("Range")

function Guide(token, status, plr)
	if status == "ON" and (plr.Character:WaitForChild("HumanoidRootPart").CFrame.Position - token.CFrame.Position).Magnitude <= Range then
		
		token.BrickColor = BrickColor.new("Neon green")
		
	elseif token:FindFirstChild("TokenHighlight") then
		token.BrickColor = BrickColor.new("Really red")
	end
end


script.Parent.WearHat.OnServerEvent:Connect(function(plr, status)
	if plr.Character:WaitForChild("Humanoid") then
		for i, token in pairs(workspace.Tokens:GetChildren()) do
			Guide(token, status, plr)
		end
	end
end)

now im just stuck with what to do

I MANAGED TO FIX IT! It was the parameters all along! And I got a error alongside it…

CFrame is not a valid member of Highlight "Workspace.Tokens.Highlight"
TokenHighlight is not a valid member of Highlight "Workspace.Tokens.Highlight" 
local Hat = game.ReplicatedStorage:WaitForChild("Goggles_TOOL")

local Range = script:GetAttribute("Range")

script.Parent.Highlighter.OnServerEvent:Connect(function(plr, status)
	if plr.Character:WaitForChild("Humanoid") then

		if status == "Equip" then
			Hat:Clone().Parent = plr.Character
			Hat.Handle.ON:Play()

			for _,v in pairs(workspace.Tokens:GetChildren()) do
				if (plr.Character:WaitForChild("HumanoidRootPart").CFrame.Position - v.CFrame.Position).Magnitude <= Range then 
					local Highlight = script.TokenHighlight:Clone()
					Highlight.Parent = v
					Highlight.Adornee = v
					Highlight.Enabled = true

				end
			end

		elseif status == "Unequip" then

			plr.Character:FindFirstChild("Goggles_TOOL"):Destroy()

			for _,v in pairs(workspace.Tokens:GetChildren()) do
				v.TokenHighlight:Destroy()
			end

			Hat.Handle.OFF:Play()
		end
	end
end)

its sorted but it doesnt do anything! i appreciate all the help i got!

1 Like