Can't get a custom gun to work

Hello guys. Today I wanted to try and make a weapon model myself without taking it from toolbox using a gun script from a tutorial I watched. For some reason My gun doesn’t shoot, but my character holds it perfectly fine. The gun script is pretty old, probably from 2019. one gun that works are a union part, and one is a model with a mesh. I tried to make a “Barrel” part but it did not work. I don’t know what to do. Can anyone help me?
Photo of gun(MP5SD):
My weapon
I also made the MP5SD a union part. Still doesn’t work.
local script inside weapon:

local gun = script.Parent
local gun_shot = gun.Handle['Gun Shot']
local empty_sound = gun.Handle.clip_empty
local reload_sound = gun.Handle.Reload
local player = game.Players.LocalPlayer
local clipSize = gun:WaitForChild('Ammo').Value
local ammo = gun:WaitForChild('Ammo')

local userInput = game:GetService('UserInputService')

local mouse = game.Players.LocalPlayer:GetMouse()
mouse.Icon = 'rbxassetid://316279304'


local ReplicatedStorage = game:GetService('ReplicatedStorage')
local remoteEvent = ReplicatedStorage:WaitForChild('ShotEvent')


gun.Equipped:Connect(function(mouse)
	
	player.PlayerGui.ScreenGui.Ammo.Visible = true
	player.PlayerGui.ScreenGui.Ammo.Text = 'Ammo: ' .. tostring(ammo.Value) .. ' / ' .. tostring(gun.MaxAmmo.Value)
	
	mouse.Button1Down:Connect(function()
		if gun.Ammo.Value > 0 then
			remoteEvent:FireServer(gun.Handle.Position, mouse.Hit.p)
			gun_shot:Play()
			gun.Ammo.Value = gun.Ammo.Value - 1
		else
			empty_sound:Play()
		end
	end)
	
end)
gun.Unequipped:Connect(function()
	player.PlayerGui.ScreenGui.Ammo.Visible = false
end)

userInput.InputBegan:Connect(function(input, gameProcessed)
	
	
	if not gameProcessed then
		if input.UserInputType == Enum.UserInputType.Keyboard then
			local keycode = input.KeyCode
			if keycode == Enum.KeyCode.R then
				if gun.Ammo.Value < clipSize and gun.MaxAmmo.Value > 0 then
				reload_sound:Play()
					reload_sound.Ended:Wait()
					if gun.MaxAmmo.Value - (clipSize - gun.Ammo.Value) >= 0 then
						gun.MaxAmmo.Value = gun.MaxAmmo.Value - (clipSize - gun.Ammo.Value)
						gun.Ammo.Value = clipSize
					else
						gun.Ammo.Value = gun.Ammo.Value + gun.MaxAmmo.Value
						gun.MaxAmmo.Value = 0
						player.PlayerGui.ScreenGui.Ammo.Text = 'Ammo: ' .. tostring(ammo.Value) .. ' / ' .. tostring(gun.MaxAmmo.Value)
					end
					end
		
			end
				
		end
	end
end)

ammo.Changed:Connect(function()
	player.PlayerGui.ScreenGui.Ammo.Text = 'Ammo: ' .. tostring(ammo.Value) .. ' / ' .. tostring(gun.MaxAmmo.Value)
end)

Bulletcreate inside serverscript service:

local ReplicatedStorage = game:GetService('ReplicatedStorage')
local remoteEvent = ReplicatedStorage:WaitForChild('ShotEvent')
local ServerStorage = game:GetService('ServerStorage')

remoteEvent.OnServerEvent:Connect(function(player, gunPos, mosPos)
	
	local bullet = Instance.new('Part')
	bullet.Name = "Bullet"
	bullet.Parent = game.Workspace
	bullet.Shape = Enum.PartType.Block
	bullet.Size = Vector3.new(0.25, 0.25, 0.25)
	bullet.BrickColor = BrickColor.new('Gold')
	
	local damageScript = ServerStorage:FindFirstChild('Damage'):Clone()
	damageScript.Parent = bullet
	
	local attacker = Instance.new('StringValue')
	attacker.Name = 'Attacker'
	attacker.Parent = bullet
	attacker.Value = player.Name
	
	local distance = (mosPos - gunPos).magnitude
	local speed = 850
	bullet.CFrame = CFrame.new(gunPos, mosPos)
	bullet.Velocity = bullet.CFrame.LookVector * speed
	bullet.Orientation = Vector3.new(0, -90, 0)
	wait(0.8)
	bullet.Transparency = 1
	bullet.CanCollide = false
end)

Damage script in Serverstorage:

local bullet = script.Parent

local function player_check(otherPart)
	local humanoid = otherPart.Parent:FindFirstChild('Humanoid')
	if humanoid and humanoid.Parent.Name ~= bullet.Attacker.Value then
		humanoid:TakeDamage(30)
	end
end

bullet.Touched:Connect(player_check)

Also, forgot to mention the “ShotEvent” in ReplicatedStorage.

1 Like

Can you post the error in the output

output
Here is the output.

i know whats the problem find a part in the gun model and name it Handle make sure you follow capitalisation

I already have a part called “Handle”, its in the union part called Handle in the gun.

actually you cant put a part inside a part and make sure you weld them using weld constraint

Edit:show the explorer

They are perfectly welded, I am a rookie dev though. I don’t know what a weld constraint is.
explorer

read this about weld constraint and make sure when welding all the parts are in workspace and after weld put them back in the tool

The thing is the welding isn’t the issue, its a script. I have no idea how to fix it.

try to get a script from a free model, like the classic roblox one

I don’t want to do that I want to use my own script from a tutorial.

1 Like

try this make the gun shot sound name to be GunShot with following capitalisation and put this script in local script

local gun = script.Parent
local gun_shot = gun:WaitForChild("Handle").GunShot
local empty_sound = gun.Handle.clip_empty
local reload_sound = gun.Handle.Reload
local player = game.Players.LocalPlayer
local clipSize = gun:WaitForChild('Ammo').Value
local ammo = gun:WaitForChild('Ammo')

local userInput = game:GetService('UserInputService')

local mouse = game.Players.LocalPlayer:GetMouse()
mouse.Icon = 'rbxassetid://316279304'


local ReplicatedStorage = game:GetService('ReplicatedStorage')
local remoteEvent = ReplicatedStorage:WaitForChild('ShotEvent')


gun.Equipped:Connect(function(mouse)
	
	player.PlayerGui.ScreenGui.Ammo.Visible = true
	player.PlayerGui.ScreenGui.Ammo.Text = 'Ammo: ' .. tostring(ammo.Value) .. ' / ' .. tostring(gun.MaxAmmo.Value)
	
	mouse.Button1Down:Connect(function()
		if gun.Ammo.Value > 0 then
			remoteEvent:FireServer(gun.Handle.Position, mouse.Hit.p)
			gun_shot:Play()
			gun.Ammo.Value = gun.Ammo.Value - 1
		else
			empty_sound:Play()
		end
	end)
	
end)
gun.Unequipped:Connect(function()
	player.PlayerGui.ScreenGui.Ammo.Visible = false
end)

userInput.InputBegan:Connect(function(input, gameProcessed)
	
	
	if not gameProcessed then
		if input.UserInputType == Enum.UserInputType.Keyboard then
			local keycode = input.KeyCode
			if keycode == Enum.KeyCode.R then
				if gun.Ammo.Value < clipSize and gun.MaxAmmo.Value > 0 then
				reload_sound:Play()
					reload_sound.Ended:Wait()
					if gun.MaxAmmo.Value - (clipSize - gun.Ammo.Value) >= 0 then
						gun.MaxAmmo.Value = gun.MaxAmmo.Value - (clipSize - gun.Ammo.Value)
						gun.Ammo.Value = clipSize
					else
						gun.Ammo.Value = gun.Ammo.Value + gun.MaxAmmo.Value
						gun.MaxAmmo.Value = 0
						player.PlayerGui.ScreenGui.Ammo.Text = 'Ammo: ' .. tostring(ammo.Value) .. ' / ' .. tostring(gun.MaxAmmo.Value)
					end
					end
		
			end
				
		end
	end
end)

ammo.Changed:Connect(function()
	player.PlayerGui.ScreenGui.Ammo.Text = 'Ammo: ' .. tostring(ammo.Value) .. ' / ' .. tostring(gun.MaxAmmo.Value)
end)
1 Like

It works now, thank you very much!

im glad it works and a pro tip dont use names with space in them use underscore or make them together because its easier script

3 Likes