Gun script not work

Hello everyone,

i been coding a gun for a while and im having a problem with one script

can anyone help me?

local maxAmmo = 10
local Ammo = maxAmmo
local reloading = false
local Player = game.Players.PlayerAdded
local PlayerGui = Player:WaitForChild(“PlayerGui”)
local textLabel = PlayerGui:WaitForChild(“AmmoDisplay”):FindFirstChild(“AmmoText”)

script.Parent.Equipped:Connect(function(Mouse)
local function reload()
reloading = true
wait(1)
Ammo = maxAmmo
reloading = false
end

script.Parent.Activated:Connect(function()
	if Ammo > 0 and not reloading then
		Ammo = Ammo - 1
		script.Parent.Gunshot:Play()
		if mouse.Target.Parent:FindFirstChild("Humanoid")then
			script.Parent.DealDamage:FireServer(Mouse.Target.Parent, 20)
		end
	elseif reloading == false then
		(reload()
			script.Parent.GunShot:Stop()
	end
	
	while wait()do
		textLabel.Text = (Ammo).."/"..maxammo
			end
end)

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)

2 Likes

are there any output errors? if so I would love to see them because it helps me a lot.

sure dev bug

Ok can I see your script?
It would help a lot

local maxAmmo = 10
local Ammo = maxAmmo
local reloading = false
local Player = game.Players.PlayerAdded
local PlayerGui = Player:WaitForChild(“PlayerGui”)
local textLabel = PlayerGui:WaitForChild(“AmmoDisplay”):FindFirstChild(“AmmoText”)

script.Parent.Equipped:Connect(function(Mouse)
local function reload()
reloading = true
wait(1)
Ammo = maxAmmo
reloading = false
end

script.Parent.Activated:Connect(function()
	if Ammo > 0 and not reloading then
		Ammo = Ammo - 1
		script.Parent.Gunshot:Play()
		if mouse.Target.Parent:FindFirstChild("Humanoid")then
			script.Parent.DealDamage:FireServer(Mouse.Target.Parent, 20)
		end
	elseif reloading == false then
		(reload()
			script.Parent.GunShot:Stop()
	end
	
	while wait()do
		textLabel.Text = (Ammo).."/"..maxammo
			end
end)

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)

1 Like

Aha! I see. When using a function which in this case is script.Parent.Activated:Connect(function(), your end needs to have ) so your error is that end should be end)

Does this work? Please tell me if so.

sadly no :frowning_face: i see no output error

is this supposed to be (reload() or reload()

hmmmm i think no ima try that :thinking:

1 Like

hey remember you said to change the end to end) it has a error

What error because that should be right.

image

should i give you the whole script? in image

You need Two ends thats another error

script.Parent.Equipped:Connect(function(Mouse)
local function reload()
reloading = true
wait(1)
Ammo = maxAmmo
reloading = false
end

should be

script.Parent.Equipped:Connect(function(Mouse)
local function reload()
reloading = true
wait(1)
Ammo = maxAmmo
reloading = false
end
end)

and if possible, I would love the full script

local maxAmmo = 10
local Ammo = maxAmmo
local reloading = false
local Player = game.Players.PlayerAdded
local PlayerGui = Player:WaitForChild(“PlayerGui”)
local textLabel = PlayerGui:WaitForChild(“AmmoDisplay”):FindFirstChild(“AmmoText”)

script.Parent.Equipped:Connect(function(Mouse)
local function reload()
reloading = true
wait(1)
Ammo = maxAmmo
reloading = false
end
end)

script.Parent.Activated:Connect(function()
	if Ammo > 0 and not reloading then
		Ammo = Ammo - 1
		script.Parent.Gunshot:Play()
		if mouse.Target.Parent:FindFirstChild("Humanoid")then
			script.Parent.DealDamage:FireServer(Mouse.Target.Parent, 20)
		end
	elseif reloading == false then
		reload()
			script.Parent.GunShot:Stop()
	end
	
	while wait() do
		textLabel.Text = (Ammo).."/"..maxAmmo
			end
end)

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)

Try this I think it will work like it is supposed to

having problems at local PlayerGui = Player:WaitForChild(“PlayerGui”)

(player:WaitForChild)