Remote Event not firing even after being put in the correct spot

Hello fellow developers! Straight to the point here: remote event does not fire… Could anybody assist me? Everything looks correct, right? Also, could someone explain why the first argument in “ColorChange” function has to be the player?

local script: (inside StarterPlayerScripts)

local PinkPart = game.Workspace:WaitForChild("TouchedRemotePart")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PinkRemoteEvent = ReplicatedStorage:FindFirstChild("PinkRemoteEvent")


PinkPart.Touched:Connect(function()
	local colorRed = BrickColor.new("Really red")
	PinkRemoteEvent:FireServer(colorRed)
end)

Server script: (inside ServerScriptService)

local PinkPart = game.Workspace:WaitForChild("TouchedRemotePart")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PinkRemoteEvent = ReplicatedStorage:FindFirstChild("PinkRemoteEvent")

local function colorChange(player, colorRed)
	PinkPart.BrickColor = colorRed
end


PinkRemoteEvent.OnServerEvent:Connect(colorChange)

Change this to WaitForChild, the client does not load all instances before scripts run, so the remote may be found as nil. Even then, it looks like you could just listen for the part touch on the server instead of using a remote.

The first parameter to colorChange is the player that fired the remote. This is because when RemoteEvents fire, they pass the player that fired it along with the sent parameters to the callback function, so then your second parameter, the colour, ends up being what you sent as the first parameter to :FireServer().

1 Like

It works now! But I got another question, which place for the local script will be better: StarterPlayerScripts or StarterCharacterScripts? Whats the difference?

StarterCharacterScripts means the script will be parented to the character. It’s generally better for localised, character related things, for example a script to control first person where you can see your character. StarterPlayerScripts should then be for anything related to the player that needs to be done on the client, but is not related to gui, because that’s what local scripts in StarterGui are for.

1 Like

Thanks for the help and explanation!

1 Like

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