How to make a script to detect a gun firing (for GUI)?
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
001 | local RayModule = require(game:GetService( "ReplicatedStorage" ).ModuleScripts:WaitForChild( "RayModule" ) ) |
002 | local HurtEvent = game:GetService( "ReplicatedStorage" ):WaitForChild( "HurtHumanoid" ) |
003 | local RunService = game:GetService( 'RunService' ) |
004 | local KeyframeSequenceProvider = game:GetService( "KeyframeSequenceProvider" ) |
005 | local UserInputService = game:GetService( "UserInputService" ) |
008 | local tool = script.Parent |
009 | local CoolDown = false |
012 | local function createPreviewAnimation(keyframeSequence) |
013 | local hashId = KeyframeSequenceProvider:RegisterKeyframeSequence(keyframeSequence) |
015 | local Animation = Instance.new( "Animation" ) |
016 | Animation.AnimationId = hashId |
024 | local spaceHeld = UserInputService:IsKeyDown(Enum.KeyCode.Space) |
027 | local sound 1 = script.Parent:WaitForChild( "Sounds" ):WaitForChild( "shoot1" ) |
028 | local headSound = script.Parent:WaitForChild( "Sounds" ):WaitForChild( "head" ) |
029 | local bodySound = script.Parent:WaitForChild( "Sounds" ):WaitForChild( "body" ) |
033 | local idle = script.Parent.Animations:WaitForChild( "idle_1" ) |
034 | local shoot = script.Parent.Animations:WaitForChild( "shoot_1" ) |
035 | local reload = script.Parent.Animations:WaitForChild( "reload_1" ) |
038 | local Cam = game.Workspace.CurrentCamera |
039 | local GunModel = script.Parent.gunModel |
040 | local AniControler = GunModel.AnimationController |
042 | local idle = AniControler:LoadAnimation(createPreviewAnimation(idle)) |
043 | local shoot = AniControler:LoadAnimation(createPreviewAnimation(shoot)) |
044 | local reload = AniControler:LoadAnimation(createPreviewAnimation(reload)) |
048 | RunService.RenderStepped:Connect( function () |
049 | GunModel:SetPrimaryPartCFrame(Cam.CFrame * CFrame.new( 0 ,- 0.3 ,- 1.2 )) |
054 | function ToolActivated() |
055 | local finalRay = RayModule.fireRay(game.Players.LocalPlayer, 500 , GunModel) |
057 | if finalRay ~ = nil then |
058 | Humanoid = finalRay.Parent:FindFirstChild( "Humanoid" ) |
062 | if (CoolDown = = false ) then |
064 | sound 1. TimePosition = 0 |
068 | if (Humanoid ~ = nil ) then |
069 | if (finalRay.Name = = "Head" ) then |
070 | HurtEvent:FireServer(Humanoid, 85 ) |
071 | local NewHead = headSound:Clone() |
072 | NewHead.Parent = finalRay |
075 | HurtEvent:FireServer(Humanoid, 15 ) |
076 | local NewBody = bodySound:Clone() |
077 | NewBody.Parent = finalRay |
093 | if UserInputService:IsKeyDown(Enum.KeyCode.R) then |
099 | UserInputService.InputBegan:Connect(ReloadAni) |
100 | 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
1 | local UserInputService = game:GetService( "UserInputService" ) |
2 | local status = game.ReplicatedStorage:WaitForChild( "Status" ) |
3 | local tool = game.StarterPack.DevGun |
6 | status.Value = Ammo - 1 |
any help is much appreciated thanks -proplayer700