Changing Color Trail applies to all characters instead of just the one who fired

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? I am trying to use a GUI that applies a color on a trail that is attached to the character through Remote Events, BUT instead of applying to the player who fires the event, it fires to all characters.

  2. What is the issue? The event is changing trail colors for all players instead of just the one who fired the event.

  3. What solutions have you tried so far? I’ve tried changing the color after the trail is applied, trying different parts of the color change event in the trail adding event.

The SERVER Script that applies the trail

local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

-- Items
local trailParts = {'Head', 'HumanoidRootPart','LeftLowerArm','LeftLowerLeg','RightLowerArm','RightLowerLeg','LeftHand','RightHand','LowerTorso','UpperRightArm','UpperLeftArm', 'UpperTorso', 'LeftFoot', 'RightFoot'}
local trail = ReplicatedStorage:WaitForChild("Trail")
local aura = ReplicatedStorage:WaitForChild("Aura")
local trailevent = ReplicatedStorage:WaitForChild("FireTrail")
local change = ReplicatedStorage:WaitForChild("Change")

--idk
game.StarterPlayer.EnableMouseLockOption = false

local red, green, blue = nil, nil, nil

trailevent.OnServerEvent:connect(function(player, action)
	local char = player.Character
	local humanoid = char:WaitForChild("Humanoid")
-- PART WHERE IT CHANGES ALL COLORS OF TRAILS WHEN ITS ONLY SUPPOSED TO APPLY TO ONE PLR
change.OnServerEvent:Connect(function(plr, color)
		local character = plr.Character or plr.CharacterAdded:Wait()
		red, green, blue = math.round(color.R * 255), math.round(color.G * 255), math.round(color.B * 255)
	end)
-- PART WHERE IT CHANGES ALL COLORS OF TRAILS WHEN ITS ONLY SUPPOSED TO APPLY TO ONE PLR
	-- checks if false or true
	if action == true then
		for index, part in pairs(char:GetChildren()) do
			if part:IsA('BasePart') then
				if table.find(trailParts, part.Name) then
					if not part:FindFirstChildWhichIsA('Trail') or part:FindFirstChildWhichIsA('ParticleEmitter') then
						local attachment0 = Instance.new('Attachment', part)
						local attachment1 = Instance.new('Attachment', part)
						attachment0.Position = Vector3.new(0, 0, 3)
						attachment1.Position = attachment0.Position + Vector3.new(0, 0.25, -6)
						
						local Aura = aura:Clone()
						Aura.Parent = part

						local newTrail = trail:Clone()
						newTrail.Parent = part
						newTrail.Enabled = true

						newTrail.Attachment0 = attachment0
						newTrail.Attachment1 = attachment1
						newTrail.Color = ColorSequence.new{
							ColorSequenceKeypoint.new(0, Color3.fromRGB(red, green, blue)),
							ColorSequenceKeypoint.new(1, Color3.fromRGB(red, green, blue))
						}
					end
				end
			end
		end
	elseif action == false then
		for index, part in pairs(char:GetChildren()) do
			if part:FindFirstChildWhichIsA('Trail') or part:FindFirstChildWhichIsA('ParticleEmitter')then
				local oldTrail = part:FindFirstChildWhichIsA('Trail')
				local oldAura = part:FindFirstChildWhichIsA('ParticleEmitter')
				oldAura:Destroy()
				oldTrail.Enabled = false
				oldTrail:Destroy()
				if humanoid.WalkSpeed < 50 then
					oldTrail:Destroy()
					oldAura:Destroy()
				end
				repeat
					if part:FindFirstChild('Attachment') then
						part:FindFirstChild('Attachment'):Destroy()
					end
				until not part:FindFirstChild('Attachment')
			end
		end
	end
end)

--[[local function addTrail(v, r, g, b)
	local char = v.Parent
	local attachment0 = Instance.new('Attachment', v)
	local attachment1 = Instance.new('Attachment', v)
	attachment0.Position = Vector3.new(0, 0, 3)
	attachment1.Position = attachment0.Position + Vector3.new(0, 0.25, -6)

	local Aura = aura:Clone()
	Aura.Parent = v

	local newTrail = trail:Clone()
	newTrail.Color = ColorSequence.new{
		ColorSequenceKeypoint.new(0, Color3.fromRGB(r, g, b)),
		ColorSequenceKeypoint.new(1, Color3.fromRGB(r, g, b))
		}
	newTrail.Parent = v
	newTrail.Enabled = true

	newTrail.Attachment0 = attachment0
	newTrail.Attachment1 = attachment1

	return newTrail, attachment0, attachment1
end]]

--[[change.OnServerEvent:Connect(function(plr, color)
	local char = plr.Character
	r, g, b = math.round(color.R * 255), math.round(color.G * 255), math.round(color.B * 255)
	print(char, r, g, b)
	
	for i, v in char:GetDescendants() do
		if v:IsA("Trail") then
			v.Color = ColorSequence.new{
				ColorSequenceKeypoint.new(0, Color3.fromRGB(r, g, b)),
				ColorSequenceKeypoint.new(1, Color3.fromRGB(r, g, b))
			}
		end
	end
end)

change.OnServerEvent:Connect(function(plr, color)
	r, g, b = math.round(color.R * 255), math.round(color.G * 255), math.round(color.B * 255) 
end)

Aura.Color = ColorSequence.new{
	ColorSequenceKeypoint.new(0, Color3.fromRGB(r, g, b)),
	ColorSequenceKeypoint.new(1, Color3.fromRGB(r, g, b))
}
newTrail.Color = ColorSequence.new{
	ColorSequenceKeypoint.new(0, Color3.fromRGB(r, g, b)),
	ColorSequenceKeypoint.new(1, Color3.fromRGB(r, g, b))
}]]

The LOCAL Script that fires the event to change the color of the trail. (Color changes due to 3 values of 3 Textboxes where u can input numbers 1-255 for a rgb value :+1:)

local spp = script.Parent.Parent.Parent
local players = game:GetService("Players")
local player = players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()

script.Parent.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.Change:FireServer(Color3.fromRGB(spp.ARED.RED.Text,spp.BGREEN.GREEN.Text,spp.CBLUE.BLUE.Text))
end)

while task.wait() do
	script.Parent.BackgroundColor3 = Color3.fromRGB(spp.ARED.RED.Text,spp.BGREEN.GREEN.Text,spp.CBLUE.BLUE.Text)
end

Another LOCAL Script that fires the event to apply the trail when LeftShift is held

-- Services
local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")

--Player
local player = game.Players.LocalPlayer
local char = player.Character
local cam = game.Workspace.CurrentCamera

--Items
local isrunning = false
local cooldown = false
local cdtimer = 3
local trailevent = ReplicatedStorage:WaitForChild("FireTrail")
local humpart = char:WaitForChild("HumanoidRootPart")
local startlightning = script.sound
local sounds = script.sounds
humpart:WaitForChild("Running").Volume = 0

-- starting behaviour
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
UserInputService.MouseDeltaSensitivity = 0.1
char.Humanoid.WalkSpeed = 5
char:WaitForChild("HumanoidRootPart").CustomPhysicalProperties = PhysicalProperties.new(1000, 0, 0)


-- tweens
local goalforrun = {}
goalforrun.FieldOfView = 90

local tweeninfoforrun = TweenInfo.new(1)

local tweenforrun = TweenService:Create(cam, tweeninfoforrun, goalforrun)

local goalforwalk = {}
goalforwalk.FieldOfView = 70

local tweeninfoforwalk = TweenInfo.new(1)

local tweenforwalk = TweenService:Create(cam, tweeninfoforwalk, goalforwalk)

UserInputService.InputBegan:Connect(function(player)
	if player.KeyCode == Enum.KeyCode.LeftShift then
		if cooldown == true then return end
		local Humanoid = char:WaitForChild("Humanoid")
		if Humanoid.MoveDirection.Magnitude > 0 then
			isrunning = true
			if isrunning == true then
				cooldown = false
				game.Workspace.Gravity = 90
				trailevent:FireServer(true)
				Humanoid.WalkSpeed = 200
				tweenforrun:Play()
			end
		end
	end
	task.wait(cdtimer)
	cooldown = false
end)

UserInputService.InputEnded:Connect(function(player)
	if player.KeyCode == Enum.KeyCode.LeftShift then
		local Humanoid = char:WaitForChild("Humanoid")
		isrunning = false
		if isrunning == false then
			game.Workspace.Gravity = 196.2
			trailevent:FireServer(false)
			Humanoid.WalkSpeed = 5
			tweenforwalk:Play()
			for i, v in pairs(sounds:GetChildren()) do
				v:Stop()
			end
		end
	end
end)
1 Like

Right at the start of the server script you connecting a remote event inside of a remove event. This essentially means that everytime any player pushes their button, they make connections for every player on the server. Not only is every player changing the colors, they are probably doing it 100’s of times.

If I were to put the Color Change Remote Event before the Trail Adding Event, the same outcome still happens where when one player selects a color, but instead of just being applied to the character who fired the event, it applies to all of them.

local red, green, blue = 255, 50, 25

change.OnServerEvent:Connect(function(plr, color)
	local character = plr.Character or plr.CharacterAdded:Wait()
	red, green, blue = math.round(color.R * 255), math.round(color.G * 255), math.round(color.B * 255)
end)

trailevent.OnServerEvent:connect(function(player, action)
	local char = player.Character
	local humanoid = char:WaitForChild("Humanoid")
	-- checks if false or true
	if action == true then
		for index, part in pairs(char:GetChildren()) do
			if part:IsA('BasePart') then
				if table.find(trailParts, part.Name) then
					if not part:FindFirstChildWhichIsA('Trail') or part:FindFirstChildWhichIsA('ParticleEmitter') then
						local attachment0 = Instance.new('Attachment', part)
						local attachment1 = Instance.new('Attachment', part)
						attachment0.Position = Vector3.new(0, 0, 3)
						attachment1.Position = attachment0.Position + Vector3.new(0, 0.25, -6)
						
						local Aura = aura:Clone()
						Aura.Parent = part

						local newTrail = trail:Clone()
						newTrail.Parent = part
						newTrail.Enabled = true

						newTrail.Attachment0 = attachment0
						newTrail.Attachment1 = attachment1
						newTrail.Color = ColorSequence.new{
							ColorSequenceKeypoint.new(0, Color3.fromRGB(red, green, blue)),
							ColorSequenceKeypoint.new(1, Color3.fromRGB(red, green, blue))
						}
					end
				end
			end
		end
	elseif action == false then
		for index, part in pairs(char:GetChildren()) do
			if part:FindFirstChildWhichIsA('Trail') or part:FindFirstChildWhichIsA('ParticleEmitter')then
				local oldTrail = part:FindFirstChildWhichIsA('Trail')
				local oldAura = part:FindFirstChildWhichIsA('ParticleEmitter')
				oldAura:Destroy()
				oldTrail.Enabled = false
				oldTrail:Destroy()
				if humanoid.WalkSpeed < 50 then
					oldTrail:Destroy()
					oldAura:Destroy()
				end
				repeat
					if part:FindFirstChild('Attachment') then
						part:FindFirstChild('Attachment'):Destroy()
					end
				until not part:FindFirstChild('Attachment')
			end
		end
	end
end)

Yes, but that’s a start. You are only doing the wrong thing once, instead of an infinite number of times!

What you need is a table that stores people’s color selections (remember to clean up the table as people leave).

The red, green and blue variables are global. Does it apply to them all after one player uses it while another player uses it? It might be a good idea to do it like this: players[plr][red] = … Using a array named players to store all of the info for all of the players separately. Of course if you do it this way you will have to clean up players[player] when they leave the game to prevent a memory leak. Honestly, it still should not be doing that anyway but maybe that will help. :slightly_smiling_face: