print(direction) is line 26, print(raycastresult) is line 28 for the server script, and print(hit) is line 42 for server script
I am trying to make a gun system by passing on the mouse hit position through a remote event. That seems to go well but then the raycast most of the time is nil and only sometimes hitting for some reason? I made parts spawn on the mouse position whenever the tool activates so I’m not sure why the raycast is still nil if the mouse is hitting something?
Local script
local player = game:GetService("Players").LocalPlayer -- mouse
local mouse = player:GetMouse()
local tool = script.Parent
local events = tool:WaitForChild("Events")
local fireevent = events:WaitForChild("FireEvent")
local db = false
tool.Activated:Connect(function()
if db == true then
return end
db = true
local hit = mouse.Hit.Position
local part = Instance.new("Part")
part.Size = Vector3.new(1,1,1)
part.Parent = workspace
part.Position = hit
part.Transparency = 0.5
part.Anchored = true
fireevent:FireServer(hit)
wait(.25)
db = false
end)
Server script
local character = nil
local serverscriptservice = game:GetService("ServerScriptService")
local tool = script.Parent
local muzzle = tool.Muzzle
local events = tool.Events
local sounds = tool.Sounds
local fireevent = events.FireEvent
local sound = sounds.Sound
local Guns = require(serverscriptservice.Guns)
tool.Equipped:Connect(function()
character = tool.Parent
end)
local function raycastmaker(hit)
local raycastparams = RaycastParams.new()
raycastparams.FilterType = Enum.RaycastFilterType.Exclude
raycastparams.FilterDescendantsInstances = {character}
local direction = (hit - muzzle.Position)
print(direction)
local raycastresult = workspace:Raycast(muzzle.Position, direction, raycastparams)
print(raycastresult)
if raycastresult then
local hitinstance = raycastresult.Instance
local hitposition = raycastresult.Position
sound:Play()
Guns.bulletvisual(muzzle, hitposition)
Guns.damagehandler(hitinstance)
end
end
fireevent.OnServerEvent:Connect(function(player, hit)
raycastmaker(hit)
print(hit)
end)
Module Script
local Guns = {}
Guns.bulletvisual = function(muzzle, hitposition)
local debris = game:GetService("Debris")
local beam = Instance.new("Beam")
local attachment0 = Instance.new("Attachment")
local attachment1 = Instance.new("Attachment")
local part = Instance.new("Part")
part.CanCollide = false
part.CanTouch = false
part.CanQuery = false
part.Size = Vector3.new(1,1,1)
part.Transparency = 1
part.Anchored = true
part.Name = "AttachmentPart"
part.Parent = workspace
attachment0.Parent = muzzle
attachment1.Parent = part
beam.Parent = workspace
beam.Attachment0 = attachment0
beam.Attachment1 = attachment1
beam.Color = ColorSequence.new(Color3.fromRGB(255,255,0))
beam.LightInfluence = 0
beam.Transparency = NumberSequence.new(0)
beam.Brightness = 10
beam.Width0 = 0.1
beam.Width1 = 0.1
part.Position = hitposition
debris:AddItem(beam, 0.1)
debris:AddItem(attachment0, 0.1)
debris:AddItem(attachment1, 0.1)
debris:AddItem(part, 0.1)
end
Guns.damagehandler = function(hitinstance)
local humanoid = hitinstance.Parent:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid:TakeDamage(20)
end
end
return Guns