Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

The gun appears to not damage players, how can I fix It?

I made a gun that has 3 ammo, which one-shots, however It appears to not damage a player nor NPC whenever the user clicks on a player or NPC with the gun equipped

Script (Main script, local script):

local maxammo = 3
local ammo = maxammo
local reloading = false
local plr = game.Players.LocalPlayer
local plrgui = plr:WaitForChild("PlayerGui")
local text = plrgui:WaitForChild("Ammo"):FindFirstChild("AmmoCount")
local ShootingAnimation = script.Parent.Animation
local ReloadAnimation = script.Parent.Reload

local function reload()
	reloading = true
	script.Parent.reloadingsound:Play() -- change reloadingsound to what ever your reload sound is named
	local Char = script.Parent.Parent
	local Hum = Char.Humanoid

	local AnimationTrack = Hum:LoadAnimation(ReloadAnimation)
	AnimationTrack:Play()
	wait(1)
	ammo = maxammo
	reloading = false
end

script.Parent.Activated:Connect(function(Mouse)
	if ammo > 0 and not reloading then
		local Char = script.Parent.Parent
		local Hum = Char.Humanoid

		local AnimationTrack = Hum:LoadAnimation(ShootingAnimation)
		AnimationTrack:Play()
		ammo = ammo - 1
		script.Parent.gunshot:Play() -- change gunshot to what ever your gun shot sound is named
		if Mouse.Target.Parent:FindFirstChild("Humanoid") then
			script.Parent.DealDamage:FireServer(Mouse.Target.Parent, 30) -- change the 25 to what ever amount of damage you want to deal
		end
	elseif reloading == false then
		reload()
		script.Parent.gunshot:Stop()-- change gunshot to what ever your gun shot sound is named
	end

	while wait() do
		text.Text = (ammo).." / "..maxammo
	end
end)


script.Parent.Equipped:Connect(function()
	local input = game:GetService("UserInputService")
	input.InputBegan:Connect(function(Key)
		if Key.KeyCode == Enum.KeyCode.R and reloading == false and ammo ~= maxammo then
			reload()
		end
	end)
end)

script.Parent.Equipped:Connect(function()
	game.Players.LocalPlayer.PlayerGui.Ammo.Enabled = true
	script.Parent.Unequipped:Connect(function()
		game.Players.LocalPlayer.PlayerGui.Ammo.Enabled = false
	end)
end)

Script (Damage script, server script):

script.Parent.DealDamage.OnServerEvent:Connect(function(player, Target, Damage)
	Target.Humanoid:TakeDamage(Damage)
end)

1 answer

The event Tool.Activated dors not return a Mouse. Tool.Equipped only returns a mouse. So you either use local Mouse = game.Players.LocalPlayer:GetMouse() inside Tool.Activated, or change the Tool.Activated event to a function called shoot() and run the shoot function inside Tool.Equipped.

First Option:


local maxammo = 3
local ammo = maxammo
local reloading = false
local plr = game.Players.LocalPlayer
local plrgui = plr:WaitForChild("PlayerGui")
local text = plrgui:WaitForChild("Ammo"):FindFirstChild("AmmoCount")
local ShootingAnimation = script.Parent.Animation
local ReloadAnimation = script.Parent.Reload

local function reload()
    reloading = true
    script.Parent.reloadingsound:Play() -- change reloadingsound to what ever your reload sound is named
    local Char = script.Parent.Parent
    local Hum = Char.Humanoid

    local AnimationTrack = Hum:LoadAnimation(ReloadAnimation)
    AnimationTrack:Play()
    wait(1)
    ammo = maxammo
    reloading = false
end

script.Parent.Activated:Connect(function()
local Mouse = game.Players.LocalPlayer:GetMouse()
    if ammo > 0 and not reloading then
        local Char = script.Parent.Parent
        local Hum = Char.Humanoid

        local AnimationTrack = Hum:LoadAnimation(ShootingAnimation)
        AnimationTrack:Play()
        ammo = ammo - 1
        script.Parent.gunshot:Play() -- change gunshot to what ever your gun shot sound is named
        if Mouse.Target.Parent:FindFirstChild("Humanoid") then
            script.Parent.DealDamage:FireServer(Mouse.Target.Parent, 30) -- change the 25 to what ever amount of damage you want to deal
        end
    elseif reloading == false then
        reload()
        script.Parent.gunshot:Stop()-- change gunshot to what ever your gun shot sound is named
    end

    while wait() do
        text.Text = (ammo).." / "..maxammo
    end
end)

script.Parent.Equipped:Connect(function(Mouse)
    local input = game:GetService("UserInputService")
    input.InputBegan:Connect(function(Key)
        if Key.KeyCode == Enum.KeyCode.R and reloading == false and ammo ~= maxammo then
            reload()
        end
    end)

    game.Players.LocalPlayer.PlayerGui.Ammo.Enabled = true
end)

script.Parent.Unequipped:Connect(function()
game.Players.LocalPlayer.PlayerGui.Ammo.Enabled = false
end)

Second Option:

local maxammo = 3
local ammo = maxammo
local reloading = false
local plr = game.Players.LocalPlayer
local plrgui = plr:WaitForChild("PlayerGui")
local text = plrgui:WaitForChild("Ammo"):FindFirstChild("AmmoCount")
local ShootingAnimation = script.Parent.Animation
local ReloadAnimation = script.Parent.Reload

local function reload()
    reloading = true
    script.Parent.reloadingsound:Play() -- change reloadingsound to what ever your reload sound is named
    local Char = script.Parent.Parent
    local Hum = Char.Humanoid

    local AnimationTrack = Hum:LoadAnimation(ReloadAnimation)
    AnimationTrack:Play()
    wait(1)
    ammo = maxammo
    reloading = false
end

function shoot(Mouse)
    if ammo > 0 and not reloading then
        local Char = script.Parent.Parent
        local Hum = Char.Humanoid

        local AnimationTrack = Hum:LoadAnimation(ShootingAnimation)
        AnimationTrack:Play()
        ammo = ammo - 1
        script.Parent.gunshot:Play() -- change gunshot to what ever your gun shot sound is named
        if Mouse.Target.Parent:FindFirstChild("Humanoid") then
            script.Parent.DealDamage:FireServer(Mouse.Target.Parent, 30) -- change the 25 to what ever amount of damage you want to deal
        end
    elseif reloading == false then
        reload()
        script.Parent.gunshot:Stop()-- change gunshot to what ever your gun shot sound is named
    end

    while wait() do
        text.Text = (ammo).." / "..maxammo
    end
end)

script.Parent.Equipped:Connect(function()
    local input = game:GetService("UserInputService")
    input.InputBegan:Connect(function(Key)
        if Key.KeyCode == Enum.KeyCode.R and reloading == false and ammo ~= maxammo then
            reload()
        end
    end)

    game.Players.LocalPlayer.PlayerGui.Ammo.Enabled = true

Mouse.Button1Down:Connect(function()
shoot(Mouse)
end)
end)

script.Parent.Unequipped:Connect(function()
game.Players.LocalPlayer.PlayerGui.Ammo.Enabled = false
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.