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)