Variables value becoming nil after remote event

Im just trying to make a simple script where you click on a part and it spawns a C4 on it, everything is fine except for when I try moving the RaycastResults from client to server so the server knows where to spawn the C4, but every time I do, the value of the RaycastResults becomes nil. I know 100% that the variable value was not nil before the remote event, any help would be appreciated.

Local script:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()

local UIS = game:GetService("UserInputService")
local GuiService = game:GetService("GuiService")
local Camera = workspace.CurrentCamera
local SpawnC4 = ReplicatedStorage.RemoteEvents.RemoteEvent

local RayLength = 5

local function GetMouseTarget()
	local MousePosition = UIS:GetMouseLocation() - GuiService:GetGuiInset()
	local UnitRay = Camera:ScreenPointToRay(MousePosition.x, MousePosition.y)
	
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {Character, Camera}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	
	local RaycastResult = workspace:Raycast(UnitRay.Origin, UnitRay.Direction * RayLength, raycastParams)

	return RaycastResult
end

UIS.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		
		local MouseTarget = GetMouseTarget()
		
		if MouseTarget ~= nil and MouseTarget.Instance.Name == "BreachableDoor" then
			SpawnC4:FireServer(MouseTarget)
		end
	end
end)

Server Script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SpawnC4Signal = ReplicatedStorage.RemoteEvents.RemoteEvent

local C4clone = ReplicatedStorage.C4:Clone()

local function PlaceC4(Target)
	print (Target)
	local Door = Target.Instance
	local HitPoint = Target.Position
	C4clone.Position = Vector3.new(HitPoint)
	C4clone.Parent = Door
end

SpawnC4Signal.OnServerEvent:Connect(function(Player, Target)
	PlaceC4(Target)
end)

Are you sure it’s not an issue with the parameters? You have a Player argument on the server script and it’s not used.

This is basically like passing an instance, it can’t be passed, you need to pass all the arguments by themselves, or as a table.

1 Like

Client to server remote events already have Player as the first perimeter as default so I don’t have to put it on the client script

2 Likes

I see. Personally i would try to mess with the parameters, but that’s just a suggestion.

This code looks good to me. It might be that the result of a raycast cannot be printed. Try printing Door instead of Target. If that doesn’t work, it may be because the result of a raycast cannot be sent through server events. Instead, define Door in the local script and then send that instead of RaycastResult to the server script.

If both of these don’t work, then the door likely doesn’t exist on the server. Check to see if the door is being created through a local script.

It worked for me and the variables passed, tysm (:slight_smile:

1 Like