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

Why is this gun not damaging other players?

Asked by 2 years ago

I was trying to create a gun that would damage the other team's players. I created a SMGLocalScript inside the gun:

local remoteEvent = ReplicatedStorage:WaitForChild('FiringSMGEvent')
local shooting = false
local equipped = false
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local mouseConnection
gun.Equipped:Connect(function()
    equipped = true
    mouse.Icon = 'rbxassetid://117431027'
    mouseConnection = mouse.Button1Down:Connect(function()
        shooting = true
        while shooting and equipped do
            remoteEvent:FireServer(player.TeamColor, gun.Handle.Position, gun.Handle.Orientation, mouse.Hit.p)
            mouse.Button1Up:Connect(function()
                shooting = false
            end)
            wait(0.1)
        end
    end)
end)



gun.Unequipped:Connect(function()
    equipped = false
    mouseConnection:Disconnect()
end)

And created a BulletMaker ServerScript inside ServerScriptService:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local FiringEvent = ReplicatedStorage:WaitForChild("FiringSMGEvent")
FiringEvent.OnServerEvent:Connect(function(Player, TeamColor, GunPosition, GunOrientation, MousePosition)
    local Bullet = Instance.new("Part")
    Bullet.CanCollide=false
    Bullet.Name="Bullet"
    Bullet.Parent=game.Workspace
    Bullet.Shape=Enum.PartType.Ball
    Bullet.Size = Vector3.new(0.5,0.5,0.5)
    Bullet.BrickColor = BrickColor.new("Smoky grey")
    Bullet.CFrame = CFrame.new(GunPosition,MousePosition)
    local Speed = 500
    Bullet.Velocity=Bullet.CFrame.lookVector*Speed
    Bullet.Touched:Connect(function(Hit)
        local Humanoid = Hit.Parent:FindFirstChild("Humanoid")
        if Humanoid and Humanoid.Parent.Name~=Player.Name and game:GetService(Players).GetPlayersFromCharacters(Hit).Parent.TeamColor~=TeamColor then
            Humanoid.Health-=10
        end
    end)
    game:GetService("Debris"):AddItem(Bullet,2)
end)

When I tested the game the BulletMaker ServerScript raised an error: Argument 1 missing or nil

1
Did it mention the lines? eyad27 8 — 2y

1 answer

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

Try this for your Server Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local FiringEvent = ReplicatedStorage:WaitForChild("FiringSMGEvent")
FiringEvent.OnServerEvent:Connect(function(Player, TeamColor, GunPosition, GunOrientation, MousePosition)
    local Bullet = Instance.new("Part")
    Bullet.CanCollide=false
    Bullet.Name="Bullet"
    Bullet.Parent=game.Workspace
    Bullet.Shape=Enum.PartType.Ball
    Bullet.Size = Vector3.new(0.5,0.5,0.5)
    Bullet.BrickColor = BrickColor.new("Smoky grey")
    Bullet.CFrame = CFrame.new(GunPosition,MousePosition)
    local Speed = 500
    Bullet.Velocity=Bullet.CFrame.lookVector*Speed
    Bullet.Touched:Connect(function(Hit)
        local Humanoid = Hit.Parent:FindFirstChild("Humanoid")
        if Humanoid and Humanoid.Parent.Name~=Player.Name then
            Humanoid.Health-=10
        end
    end)
    game:GetService("Debris"):AddItem(Bullet,2)
end)
0
Alright, Thank You! sidb0102 17 — 2y
0
You're very welcome! KoalaKo_XD 27 — 2y
Ad

Answer this question