Needing URGENT help with Remote Events

Can anyone help with this script. I don’t do much scripting so it could be something simple that I’m just not doing right.

My game has a game pass that allows users to have a rainbow trail that follows them. I am trying to script a GUI button that allows them to turn the trail off or on. I started off doing it as a local script but then realized it would only show locally for that player. I need it to function globally.

Now I am using remote events in order to make to function globally but it is still acting as a local script basically.

This is the local script inside of the text button

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local trail = ReplicatedStorage:WaitForChild("trail")
local player = game:GetService("Players").LocalPlayer
local plrName = player.Name
local char = game.Workspace:WaitForChild(plrName)
local charHead = char:WaitForChild("Head")


script.Parent.MouseButton1Click:Connect(function()
	trail:FireServer()
	charHead.Trail.Enabled = false
	

end)
	

This is the server script inside of ServerScriptService

local ReplicatedStorage = game.ReplicatedStorage
local remoteEvent = ReplicatedStorage:WaitForChild("trail")
local char = game.Players.LocalPlayer
local charHead = char:WaitForChild("Head")

remoteEvent.OnServerEvent:Connect(function(player)
	charHead.Trail.Enabled = false

end)

Inside of Replicated Storage I have the remote event called “trail”

You can’t access LocalPlayer through a server script, as it is a server script. Within the event, on the server side, you would want it to be something like this:

remoteEvent.OnServerEvent:Connect(function(player)
	local charHead = workspace:WaitForChild(player.Name).Head
	charHead.Trail.Enabled = false
end)

Since it is a server script, it has no idea what a LocalPlayer is, thus not allowing the code within the event to properly work. Just always keep in mind that the server cannot see what the player sees, as they are two separate objects.

6 Likes

Also if you change values on a local script compared to server script the values will not be visible on local script if you were to check later on. So really if you make a trail invisible on the local script everyone else can still see the trail.

Thank you! It’s working properly now.

1 Like

If it works mark his as the solution?

2 Likes

I would greatly appreciate the badge too xD

As what @FlyingKiyan said above, you cannot access LocalPlayer Server Side, it can only be called from the Client Side (local script only). You are also meant to be getting the player’s character’s head, not the player’s head, since that’s not possible.
I have fixed both of your scripts, the code will be shown below.
(Also, make sure you replace everything from your scripts with the code given below)

Local Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local trailEvent = ReplicatedStorage:WaitForChild("trail")

local player = game.Players.LocalPlayer

local Button = script.Parent

Button.Activated:Connect(function()
   trailEvent:FireServer()
end)

Server Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TrailEvent = ReplicatedStorage:WaitForChild("trail")

TrailEvent.OnServerEvent:Connect(function(player)
   game.Workspace:WaitForChild(player.Name).Head.Trail.Enabled = false
end)
1 Like