I’m trying to make a custom laser gun and to use LocalScript for detecting inputs and ServerScript for firing the laser. However I got an error whenever I fire the gun: attempt to index nil with 'Instance'
LocalScript
tool=script.Parent
handle=tool:WaitForChild('Handle')
shoot=tool:WaitForChild('Shoot')
-- other code
function initialShoot(part, params)
local point=handle:WaitForChild('StartingPoint')
local direction=point.CFrame:PointToObjectSpace(part.Position)
local result=workspace:Raycast(
point.WorldPosition,
direction,
params
)
if result ~= nil then
shoot:WaitForChild('Activate'):FireServer(part,result)
end
end
-- other code
ServerScript
tool=script.Parent
handle=tool:WaitForChild('Handle')
event=script:WaitForChild('Activate')
players=game:GetService('Players')
-- other code
event.OnServerEvent:Connect(function(player, part, result)
local character=part.Parent
local target=players:GetPlayerFromCharacter(character)
if target then
if target~=player and result.Instance==part then -- WHERE IT ERRORS
local humanoid=character:WaitForChild('Humanoid')
humanoid.Health-=25
end
-- other code
end
Hireachy
Either the RaycastResult is nil or the RemoteEvent is having issues passing the arguments. But I made sure if RaycastResult is nil then it won’t fire the event. Any ideas?