RemoteEvent not working

Yeah so i wanted to create a Spawn GUI for trains. When touching a part, the GUI gets copied over to the player’s playergui. But when the Player effectively presses the Spawn button, the remote event gets triggered, a error gets printed attempt to call a nil value. I’m trying to use a String as Argument.

The Script which gets fired trough the RemoteEvent is following:

local Lightning = game:GetService("Lighting")
local TrainType = nil
local player = nil

local RemoteEvent = script.Parent.SpawnPlace1

-- list all models
local HK4CarriagesModel = game.Lighting["Ge 4/4 II Road Edition"]
local HK8CarriagesModel = game.Lighting["Ge 4/4 II Road Edition Blue"]

local function SpawnTrain(player, TrainType)
	
print(typeof(player), typeof(TrainType))	
	if TrainType == "HK4Carriages" then
		
		local CopyHK4 = HK4CarriagesModel:Clone()

		CopyHK4.Parent = game.Workspace
	
	elseif TrainType == "HK8Carriages" then
			
		local CopyHK8 = HK8CarriagesModel:Clone()

		CopyHK8.Parent = game.Workspace
				
		
	end
end

RemoteEvent.OnServerEvent:Connect(SpawnTrain(player,TrainType))

What line is the error happening on and can you show us the line for when the client fires the remote event?

local SpawnEvent = game.Workspace.CreateGUI.SpawnPlace1
local SpawnButton = script.Parent
local GUI = script.Parent.Parent.Parent.Parent.Parent
local player = nil

local function FireEvent()
	
	SpawnEvent:FireServer(player, "HK8Carriages")
	GUI:Destroy()
	
end

SpawnButton.MouseButton1Click:Connect(FireEvent)

thats the second of the 2 scripts, they are pretty similar, just a few variables changed

Try this:

local SpawnEvent = game.Workspace.CreateGUI.SpawnPlace1
local SpawnButton = script.Parent
local GUI = script.Parent.Parent.Parent.Parent.Parent
local player = nil

local function FireEvent()
	
	SpawnEvent:FireServer("HK8Carriages")
	GUI:Destroy()
	
end

SpawnButton.MouseButton1Click:Connect(FireEvent)

You don’t need to pass the player as an argument because the first parameter of RemoteEvent | Roblox Creator Documentation is the player that fired the remote event.

The Player variable is nil in the fireserver event. Also, you don’t need to add a player argument when firing to the server.

SpawnEvent:FireServer("HK8Carriages")

I tried this and it still prints the same error, tough there is a new one now: Attempt to connect failed: Passed value is not a function

I found another problem in your code and I fixed it.

local Lightning = game:GetService("Lighting")
local TrainType = nil
local player = nil

local RemoteEvent = script.Parent.SpawnPlace1

-- list all models
local HK4CarriagesModel = game.Lighting["Ge 4/4 II Road Edition"]
local HK8CarriagesModel = game.Lighting["Ge 4/4 II Road Edition Blue"]

local function SpawnTrain(player, TrainType)
	
print(typeof(player), typeof(TrainType))	
	if TrainType == "HK4Carriages" then
		
		local CopyHK4 = HK4CarriagesModel:Clone()

		CopyHK4.Parent = game.Workspace
	
	elseif TrainType == "HK8Carriages" then
			
		local CopyHK8 = HK8CarriagesModel:Clone()

		CopyHK8.Parent = game.Workspace
				
		
	end
end

RemoteEvent.OnServerEvent:Connect(SpawnTrain)

You’re connecting your OnServerEvent within the parameters of the SpawnTrain function on the Server’s Side, you only need to call it by it’s own function without any parameters:

local Lightning = game:GetService("Lighting")
local TrainType = nil
local player = nil

local RemoteEvent = script.Parent.SpawnPlace1

-- list all models
local HK4CarriagesModel = game.Lighting["Ge 4/4 II Road Edition"]
local HK8CarriagesModel = game.Lighting["Ge 4/4 II Road Edition Blue"]

local function SpawnTrain(player, TrainType)
	
print(typeof(player), typeof(TrainType))	
	if TrainType == "HK4Carriages" then
		
		local CopyHK4 = HK4CarriagesModel:Clone()

		CopyHK4.Parent = game.Workspace
	
	elseif TrainType == "HK8Carriages" then
			
		local CopyHK8 = HK8CarriagesModel:Clone()

		CopyHK8.Parent = game.Workspace
				
		
	end
end

RemoteEvent.OnServerEvent:Connect(SpawnTrain) -- Here is where the error lies

This brought me much further. Tho it passed the parameters as instances now, not strings → the if statement sorts them out. → nothing happens

nvm, forgot to add the player parameter in the function

thank you all for your quick and effective help!