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

How to make a script to detect a gun firing (for GUI)?

Asked by 4 years ago

Hello so me and 2 of my friends are making a shooter game we have animations and multiple guns and ray casting for the bullets

I have been assigned to making a ammo counter GUI and reload script the problem is i have never made one before and need it made by the end of today. below is my friends script

local RayModule = require(game:GetService("ReplicatedStorage").ModuleScripts:WaitForChild("RayModule") )
local HurtEvent = game:GetService("ReplicatedStorage"):WaitForChild("HurtHumanoid")
local RunService = game:GetService('RunService')
local KeyframeSequenceProvider = game:GetService("KeyframeSequenceProvider")
local UserInputService = game:GetService("UserInputService")


local tool = script.Parent
local CoolDown = false


local function createPreviewAnimation(keyframeSequence)
        local hashId = KeyframeSequenceProvider:RegisterKeyframeSequence(keyframeSequence) 
        if hashId then 
            local Animation = Instance.new("Animation")
            Animation.AnimationId = hashId
            return Animation
        end
    end




local spaceHeld = UserInputService:IsKeyDown(Enum.KeyCode.Space)

-- Sounds
local sound1 = script.Parent:WaitForChild("Sounds"):WaitForChild("shoot1")
local headSound = script.Parent:WaitForChild("Sounds"):WaitForChild("head")
local bodySound = script.Parent:WaitForChild("Sounds"):WaitForChild("body")


-- Animations
local idle = script.Parent.Animations:WaitForChild("idle_1")
local shoot = script.Parent.Animations:WaitForChild("shoot_1")
local reload = script.Parent.Animations:WaitForChild("reload_1")


local Cam = game.Workspace.CurrentCamera
local GunModel = script.Parent.gunModel
local AniControler = GunModel.AnimationController

local idle = AniControler:LoadAnimation(createPreviewAnimation(idle))
local shoot = AniControler:LoadAnimation(createPreviewAnimation(shoot))
local reload = AniControler:LoadAnimation(createPreviewAnimation(reload))

idle:Play()

RunService.RenderStepped:Connect(function()     
    GunModel:SetPrimaryPartCFrame(Cam.CFrame * CFrame.new(0,-0.3,-1.2))
end)



function ToolActivated()
    local finalRay = RayModule.fireRay(game.Players.LocalPlayer, 500, GunModel)-- Raycast from the current player, 500 studs out
    local Humanoid = nil
    if finalRay ~= nil then
        Humanoid = finalRay.Parent:FindFirstChild("Humanoid") -- Find humanoid object from player/bot
    end


    if (CoolDown == false) then -- Check if we are on cooldown
        CoolDown = true -- Set it to true, so once the function is finshed, we can start the timer
        sound1.TimePosition = 0
        sound1:Play()
        shoot:Play()

        if (Humanoid ~= nil) then
            if (finalRay.Name == "Head") then
                HurtEvent:FireServer(Humanoid,85)
                local NewHead = headSound:Clone()
                NewHead.Parent = finalRay
                NewHead:Destroy()
            else
                HurtEvent:FireServer(Humanoid,15) -- Damage Player, or bot
                local NewBody = bodySound:Clone()
                NewBody.Parent = finalRay
                NewBody:Destroy()
            end
        end
    else
        return -- If we are, Throw out click, and wait for timer
    end 

    spawn(function() -- Spawn Timer on seperate thread, then set cooldown to false
        wait(0.35)
        CoolDown = false -- Set Cooldown to false
    end)

end

function ReloadAni()
    if UserInputService:IsKeyDown(Enum.KeyCode.R) then
        reload:Play()
    end
end


UserInputService.InputBegan:Connect(ReloadAni)
tool.Activated:Connect(ToolActivated)

Now i have tried a couple of things 1 of which was to use function ToolActivated() and it does not seem to work I have it set up so that i can do ammo - 1 every time the gun is fired i just need to find a way to detect that below is my code for that

local UserInputService = game:GetService("UserInputService")
local status = game.ReplicatedStorage:WaitForChild("Status")
local tool = game.StarterPack.DevGun
local Ammo = 15

    status.Value = Ammo - 1

any help is much appreciated thanks -proplayer700

0
Everytime you shoot, use an if condition to check if it is zero or less than that. Reload if it is. radiant_Light203 1166 — 4y

Answer this question