Check for tool in player's hand breaks code | Gun system [HELP]

The gun system should have whenever the gun is present in-hand of the player, it should then be able to play the sounds upon firing after its cooldowns.

Goal

The player can only have the script run if it is in their hand. Everything works and ends well with the cooldown too, but that is until it comes for the sound to be played rbxassetid://131070686. It is a valid asset ID as it worked for whenever the player clicked (with and without the pistol).

Issue

Whenever it comes for the sound - gun shot sound effect (rbxassetid://131070686) - to be played, it won’t play the sound and it breaks my code.

Setup

I have the server script for damaging players.
That script is where the cooldown and sound resides.

Workspace Setup

Code (server)

This is the action script.
It only breaks whenever I add the if statement for tool.

local tool = game.Players.LocalPlayer.Character:FindFirstChild("Pistol")
local cooldown = false

script.Parent.Fired.OnServerEvent:Connect(function(player, target)
	if tool then
		if cooldown == false then
			if target:IsDescendantOf(player.Character) then
				print("No damage")
			else 
				if target.Parent:FindFirstChild("Humanoid") then
					if target.Name == "Head" then
						target.Parent.Humanoid.Health -= 20
						print(tostring(target.Parent).." lost 20 health points.")
					else
						target.Parent.Humanoid.Health -=10
						print(tostring(target.Parent).." lost 10 health points.")
					end				
				end
				
				script.gunShot:Play()
				cooldown = true -- enables cooldown
				wait(.5) -- cooldown is 5 seconds
				cooldown = false -- disables cooldown	

			end		
		end
	end
end)
Code (local)

This is the mouse-target script.
There is no issue here.

local mouse = game.Players.LocalPlayer:GetMouse()

mouse.Button1Down:Connect(function()
	script.Parent.Fired:FireServer(mouse.Target)
end)

Let me know if I have missed something and feel free to link any external links in assistance to solving the issue.

you don’t play the sound in the server script?
And why isnt ur tool variable just script.Parent ?

1 Like

I do play the sound in the server script. I guess I forgot to include that, but you can check the server-sided code.

I did not make tool a variable of script.Parent because it makes no sense to me. I am not checking if a tool is in the script, I am checking if the tool is in the LocalPlayer’s hands (by finding the FirstChild called “Pistol”).

That sounds stupid to me, instead do something like this:

local tool = script.Parent
local function isEquipped()
if tool.Parent:FindFirstChildWhichIsA("Humanoid") then return true else return false
       end
end

This should work, try it out

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.