Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Can you help me with my gun script? It doesn't shoot after click.

Asked by 2 years ago
Edited 2 years ago

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)
0
Does it give you any errors? keram1010 0 — 2y
0
No, except for when i shoot at the sky, then it gives me an error and then i cant use the tool anymore. FalexForojanJunior 2 — 2y
0
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. FalexForojanJunior 2 — 2y
0
show me the new code with the if check DefinitelyNotTronix 19 — 2y
View all comments (2 more)
0
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 FalexForojanJunior 2 — 2y
0
look down DefinitelyNotTronix 19 — 2y

1 answer

Log in to vote
0
Answered by 2 years ago

And What's the Error

0
it's Players.FalexForojanJunior.Backpack.Pistol.GunStart:16: attempt to index nil with 'Position' FalexForojanJunior 2 — 2y
0
its becaus eif you shoot at sky it cant get position of sky so check if target is existing DefinitelyNotTronix 19 — 2y
0
k, also there is another problem, when i shoot through a wall or when i shoot a player, it doesn't do anything FalexForojanJunior 2 — 2y
0
try on line 24 change direction to mouseposition DefinitelyNotTronix 19 — 2y
View all comments (3 more)
0
the if statement that's checking if target exists doesn't work, i tried shooting at the walls and dummies multiple times FalexForojanJunior 2 — 2y
0
and where is the if statement ??? DefinitelyNotTronix 19 — 2y
0
look at the updated server script FalexForojanJunior 2 — 2y
Ad

Answer this question