Highlight color changed if a certain object is found

hello, so i have a problem, so like i got a script in serverscriptservice which spawns a highlight in the player’s character when joining. (it works)

game:GetService("Players").PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local highlight = game.ReplicatedStorage.Highlight:Clone()
		highlight.Parent = character
		highlight.FillTransparency = 1
		highlight.OutlineTransparency = 1
	end)
end)

but now i need to make the highlight’s properties change once the script is related in some way to the character. that script is in an attachment in the player’s humanoidrootpart. (script.Parent.Parent… = char)

here is the localscript i made that changes the properties once the attachment is found but doesnt change it back when the attachment is gone.

local character = game.Players.LocalPlayer.Character
local folder = character:WaitForChild("Aura")
local highlight = character:WaitForChild("Highlight")

while true do
	if folder:FindFirstChildWhichIsA("Folder") then
		highlight.FillColor = Color3.fromRGB(9, 70, 0)
		highlight.FillTransparency = 0.5
		highlight.OutlineColor = Color3.fromRGB(9, 70, 0)
		highlight.OutlineTransparency = 0
	end
	wait()
end

thanks for your help

1 Like

btw the “Aura” folder in the character is the place where another folder will spawn when an aura/attachment is equipped

I kinda understand what you are trying to do in here. But, I can say for sure this is not a good approach to do it.

First, on a localscript you are “waiting” for an instance called “Highlight” to spawn inside “player” instance owner of this localscript.

And you placed the “Highlight” instance from a server script upon join, inside the “Character” instance of the player (meaning, the Model of the player, not the Player instance)

So you will never find it.

But. You dont have to do this in order to fire functions when player is found, got its stuff, etc, not locally or server side.

no no but i named the character’s variable player mb

?⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀

the variable that represents the character is named “player” but i can change that if u want

I suggest you change the entire approach. First explain what exactly you want to achieve.

When player joins, a Highlight instance is added to it and the colors should be this (), after, all data from player is loaded?.. maybe, the color is gotten from their Datastore?.. idk
btw, Highlight thing in my experience is very buggy, but it works fine if you use it carefully

so when the parent of the script (an attachment named “Aura”) is in the humanoidrootpart, then the highlight should change color and become visible but if theres no attachment in the humanoidrootpart, then it should turn invisible again

Use events for this.

When a child is added into the HRP of the player, if the child added is your “Aura” fire the function that will change the params of the Highlight.

Same when the child is removed.

“Depending on your system”, but using a check like a “while true” just to see if “Aura” is inside the player, its pretty bad idea

thanks ill try to make it work, im kinda bad at scripting tho lol but thanks for helping

1 Like

help i cant figure it out plss

hey so i tried something but its kinda weird since i suck at scripting but here is what i tried (it doesnt work)

local script (in attachment)

local player = game.Players.LocalPlayer
local char = player.Character
local root = char:WaitForChild("HumanoidRootPart")
local rs = game.ReplicatedStorage
local fillTransparency = 0.5
local fillColor = Color3.fromRGB(9, 70, 0)
local outlineColor = Color3.fromRGB(9, 70, 0)
local outlineTransparency = 0

root.Changed:Connect(function(player, fillTransparency, fillColor, outlineColor, outlineTransparency)
	rs.Remotes.HighlightChange:FireServer(fillTransparency, fillColor, outlineColor, outlineTransparency)
end)

server script (in serverscriptservice)

local rs = game.ReplicatedStorage
local highlight = game.ReplicatedStorage.Highlight:Clone()

--game.Players.PlayerAdded:Connect(function(player)
	--player.CharacterAdded:Connect(function(char)
		--highlight.Parent = char                             --this part just spawns the highlight upon joining
		--highlight.FillTransparency = 1
		--highlight.OutlineTransparency = 1
	--end)
--end)

rs.Remotes.HighlightChange.OnServerEvent:Connect(function(player, fillTransparency, fillColor, outlineColor, outlineTransparency)
	print(player..fillTransparency..fillColor..outlineColor..outlineTransparency)
	if player.Character:WaitForChild("HumanoidRootPart"):FindFirstChild("Aura") then
		highlight.FillTransparency = fillTransparency
		highlight.OutlineTransparency = outlineTransparency
		highlight.FillColor = fillColor
		highlight.OutlineColor = outlineColor
	else
		highlight.FillTransparency = 1
		highlight.OutlineTransparency = 1
	end
end)

i dont get any errors so idk what im doing wrong

You can do it with only one script in server without needing the local script, or the use of remotes.

When player joins, clone the Highlight instance, place it in the Character, and connect ChildAdded and ChildRemoved to the HRP to listen when the Attachment named “Aura” is added and removed from the HRP. And do the stuff you want on each case.
You could try something like this:

local Highlight = game.ReplicatedStorage:WaitForChild("Highlight")

-- Properties to change in Highlight
local AuraParams = {
	FillTransparency = 0.5,
	FillColor = Color3.fromRGB(9, 70, 0),
	OutlineColor = Color3.fromRGB(9, 70, 0),
	OutlineTransparency = 0,
	Enabled = true	
}

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		if not plr:HasAppearanceLoaded() then
			plr.CharacterAppearanceLoaded:Wait()
		end
		warn(char, "loaded")
		
		-- Create the highlight
		local newHighlight = Highlight:Clone()
		newHighlight.Parent = char
		--newHighlight.Enabled = true
		
		local HRP = char:WaitForChild("HumanoidRootPart")
		
		-- Fired everytime something is parented inside the HRP
		HRP.ChildAdded:Connect(function(aChild)
			if aChild.Name == "Aura" then -- if the added child's name is Aura do stuff
				warn("got aura")
				-- Iterate a table of properties to change Highlight params
				for param, data in pairs(AuraParams) do 
					newHighlight[param] = data
				end
			end
		end)
		
		-- Everytime a child is removed from HRP
		HRP.ChildRemoved:Connect(function(aChild) 
			if aChild.Name == "Aura" then
				newHighlight.Enabled = false
			end
		end)
	end)
end)

Im not exactly sure what you mean by “Attachment”. I do think on the instance called Attachment, a green invisible ball that is inside the parts of the character model.
Idk how you are handling that, “giving an Attachment” to the HRP of the player.

So I tested the above code with this little button on workspace that gives an Attachment to the player’s HRP just to check the above code works works.

workspace:WaitForChild("Part"):WaitForChild("ClickDetector").MouseClick:Connect(function(plr)
	local char = plr.Character
	if char then
		local HRP = char:FindFirstChild("HumanoidRootPart")
		if HRP then
			if HRP:FindFirstChild("Aura") then
				HRP.Aura:Destroy()
			else
				local newAttch = Instance.new("Attachment")
				newAttch.Name = "Aura"
				newAttch.Parent = HRP
			end
		end
	end
end)

thank you so much it worked lol

1 Like

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