Yo, i'm back after 6 months and I came across a problem with my gun script, every time I shoot at the enemy it doesn't work, here's the part of the code that i think has that problem.
Server Script UPDATED
local event = game.ReplicatedStorage.ShootGun
local damage = math.random(20,60)
local range = 50
local gunshot = script.Parent.gunshot
function createBeam(pos)
print(8)
local part = Instance.new("Part", workspace)
part.Position = pos
print(9)
local at1 = Instance.new("Attachment", part)
local at2 = Instance.new("Attachment", script.Parent.Handle)
print(10)
local beam = Instance.new("Beam", script.Parent.Handle)
beam.Texture = "http://www.roblox.com/asset/?id=1274378728"
print(11)
end
event.OnServerEvent:Connect(function(player, mousePos, originPos)
print(1)
local direction = (mousePos - originPos).Unit * range
print(2)
local result = workspace:Raycast(originPos, mousePos)
if result then
print(3)
print(4)
local character = result.Instance.Parent
local humanoid = character:FindFirstChild("Humanoid")
print(5)
if humanoid then
print(6)
humanoid:TakeDamage(damage)
end
print(7)
createBeam(result.Instance.Position)
gunshot:Play()
end
end)
Local Script
local tool = script.Parent
local origin = tool:WaitForChild("Handle").Position
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local event = game.ReplicatedStorage.ShootGun
local recoil = false
local recoilTime = 0.4
tool.Activated:Connect(function()
if recoil then return end
recoil = true
local mousePosition = mouse.Target.Position
event:FireServer(mousePosition, origin)
wait(recoil)
recoil = false
end)

6 comments
Does it give you any errors?
No, except for when i shoot at the sky, then it gives me an error and then i cant use the tool anymore.
No, except for when i shoot at the sky, then it gives me an error and then i cant use the tool anymore. Also the if result then statement doesn't work, it skips over that.
show me the new code with the if check
local event = game.ReplicatedStorage.ShootGun local damage = math.random(20,60) local range = 50 local gunshot = script.Parent.gunshot function createBeam(pos) print(8) local part = Instance.new("Part", workspace) part.Position = pos print(9) local at1 = Instance.new("Attachment", part) local at2 = Instance.new("Attachment", script.Parent.Handle) print(10) local beam = Instance.new("Beam
look down