Help With My Gun Engine

Hello! So Today Im Creating a Hood/Real Life Game Basicly Like Da Hood And I Would Like To Ask a Question.

How Do I Make My Gun Slow The Player Down Everytime The Gun Fires Even When Its Auto Or Semi + Slowing The Player Down When Reloading Too

Here’s The Code

ServerMain

local maxammo = script.Parent.MaxAmmo.Value
local ammo = script.Parent.Ammo
local reloading = false









local spread = script.Parent.EDIT.Spread.Value






script.Parent.Shoot.OnServerEvent:Connect(function (plr,Target)
	if plr.Character ~= nil and plr.Character.Humanoid.Health >= .1 and script.Parent.Enabled == true and reloading == false then
	script.Parent.Enabled = false
	local hit = false
	
	if ammo.Value >= 1 then
	ammo.Value = ammo.Value - 1
	script.Parent.Handle.FireSound:Play()
	for i = 1, script.Parent.EDIT.BulletsPerShot.Value do
	local dist = (script.Parent.RPart.Position - Target).Magnitude
	local ray = Ray.new(script.Parent.RPart.CFrame.p, (Target - script.Parent.RPart.CFrame.p + (Vector3.new(math.random(-spread,spread)/1000,math.random(-spread,spread)/1000,math.random(-spread,spread)/1000) * dist)).unit * 2000)
	local part, position = workspace:FindPartOnRay(ray, plr.Character, false, true)
	print(position)
	print(part)
	print(Target)
	local beam = Instance.new("Part")
	beam.Anchored = true
	beam.CanCollide = false
	beam.Material = "Neon"
				beam.Transparency = "0.5"	
				beam.BrickColor = BrickColor.new("Bright yellow")
	if dist >= 60 then
	beam.Size = Vector3.new( 0.042, 0.001)
	else
    beam.Size = Vector3.new(.1,.1,dist)
    end
	beam.CFrame = CFrame.new(script.Parent.RPart.CFrame.p, position) * CFrame.new(0,0,-beam.Size.Z/2)
	game.Debris:AddItem(beam,10)
	local fx = script.Parent.FX:Clone()
	fx.Dist.Value = dist
	fx.Parent = beam
	fx.Disabled = false
	beam.Parent = workspace
	script.Parent.RPart.FlashGui.Label.Rotation = math.random(1, 360)
	script.Parent.RPart.FlashGui.Enabled = true
	script.Parent.RPart.Flash.Enabled = true
	if part ~= nil then
	if part.Parent:FindFirstChild("Humanoid") then
		part.Parent:FindFirstChild("Humanoid"):TakeDamage(script.Parent.EDIT.Damage.Value)
		hit = true
	end
	end
  local hole = Instance.new("Part")
  hole.Size = Vector3.new(.2,.2,.2)
hole.Material = "Plastic"
hole.Anchored = true
hole.BrickColor = BrickColor.new("Black")
hole.Shape = 0
hole.CanCollide = false
hole.Position = position
hole.Transparency = 1	
	game.Debris:AddItem(hole, 1)
	if hit == false then
	hole.Parent = workspace
	end
    wait()
    script.Parent.RPart.FlashGui.Enabled = false
    script.Parent.RPart.Flash.Enabled = false
	end
	elseif ammo.Value <= 0 then
		script.Parent.Handle.NoAmmo:Play()
	end
	wait(script.Parent.EDIT.Firerate.Value)
	
	script.Parent.Enabled = true
	end
end)
script.Parent.Reload.OnServerEvent:Connect(function ()
	if reloading == false then
		reloading = true
		script.Parent.Handle.Reload:Play()
		wait(script.Parent.EDIT.ReloadTime.Value)
		ammo.Value = maxammo
		reloading = false
	end
end)




ClientMain

local held = false
local plr = game.Players.LocalPlayer
script.Parent.Equipped:Connect(function()
	chr = script.Parent.Parent
end)

script.Parent.Activated:Connect(function ()
	held = true
if script.Parent.Enabled == true then
	script.Parent.Enabled = false
	
	
	while held == true do
	if script.Parent.EDIT.BurstShots.Value >= 2 then
	for i = 1, script.Parent.EDIT.BurstShots.Value do
	script.Parent.Shoot:FireServer(chr.Humanoid.TargetPoint)
	fire:Play()
	wait(script.Parent.EDIT.BurstRate.Value)
	
    
	end
	else
	fire:Play()
	script.Parent.Shoot:FireServer(chr.Humanoid.TargetPoint)
	end
	if script.Parent.EDIT.Auto.Value == false then
		held = false
	end
	wait(script.Parent.EDIT.Firerate.Value + .1)
	end
	if script.Parent.EDIT.Auto.Value == false then
		wait(script.Parent.EDIT.Firerate.Value + .1)
	end
	script.Parent.Enabled = true
end
end)

script.Parent.Deactivated:Connect(function()
	held = false
	
end)
script.Parent.Equipped:Connect(function()
	wait()
	holdanim = chr.Humanoid:LoadAnimation(script.Parent.HoldAnim)
	holdanim:Play()
	rel = chr.Humanoid:LoadAnimation(script.Parent.ReloadAnim)
	fire = chr.Humanoid:LoadAnimation(script.Parent.FireAnim)
end)
script.Parent.Unequipped:Connect(function()
	held = false
	holdanim:Stop()
	fire:Stop()
	rel:Stop()
end)
game.Players.LocalPlayer:GetMouse().KeyDown:connect(function(KeyPressed)
	if KeyPressed == "r" and script.Parent.Parent == plr.Character then 
		script.Parent.Reload:FireServer()
		
		rel:Play()
	end
end)


I Really Hope You Can Help Me With This Issue.
If You Have a Solution Please Reply

1 Like

You could just make the bullet subtract the enemy.Humanoid.WalkSpeed.Value.

Bullet.Touched:Connect(function(otherPart)
    local Humanoid = otherPart.Parent:FindFirstChild("Humanoid")
    if Humanoid then
       Humanoid.WalkSpeed = 12
       task.wait(3)
       Humanoid.WalkSpeed = 16
    end
end)

Well I Wanted It To Slow Down The Player Everytime The Player Fires The Weapon, Even When Its Auto Or Semi + Slowing Down When Reloading. Not Slowing Down The Player When Theyre Shot, But I Apreciate Your Reply

1 Like

Well, in that case:

script.Parent.Shoot.OnServerEvent:Connect(function (plr,Target)
      local ownerCharacter = plr.Character
      local ownerHumanoid = ownerCharacter:FindFirstChild("Humanoid")
       
      if ownerHumanoid then
         ownerHumanoid.WalkSpeed = 12
         task.wait(3)
         ownerHumanoid.WalkSpeed = 16
      end
end)

Make it such that when the shooting event/animation is still going on, it will change the humanoid.WalkSpeed value. and if it isnt, change it back
As for reloading just check if reloading is true then change the speed value

1 Like

I’ll Try It Out To See If It Works, I Also Took Some Code From GFXBIT And Edited My Code

1 Like

Didnt Work Sadly, It Kinda Broke My Gun :frowning: Maybe Try Editing The Code I Put On The Post

1 Like
local maxammo = script.Parent.MaxAmmo.Value
local ammo = script.Parent.Ammo
local reloading = false









local spread = script.Parent.EDIT.Spread.Value






script.Parent.Shoot.OnServerEvent:Connect(function (plr,Target)
	if plr.Character ~= nil and plr.Character.Humanoid.Health >= .1 and script.Parent.Enabled == true and reloading == false then
	script.Parent.Enabled = false
	local hit = false
	
	if ammo.Value >= 1 then
	ammo.Value = ammo.Value - 1
	script.Parent.Handle.FireSound:Play()
-- slower here
	for i = 1, script.Parent.EDIT.BulletsPerShot.Value do
	local dist = (script.Parent.RPart.Position - Target).Magnitude
	local ray = Ray.new(script.Parent.RPart.CFrame.p, (Target - script.Parent.RPart.CFrame.p + (Vector3.new(math.random(-spread,spread)/1000,math.random(-spread,spread)/1000,math.random(-spread,spread)/1000) * dist)).unit * 2000)
	local part, position = workspace:FindPartOnRay(ray, plr.Character, false, true)
	print(position)
	print(part)
	print(Target)
	local beam = Instance.new("Part")
	beam.Anchored = true
	beam.CanCollide = false
	beam.Material = "Neon"
				beam.Transparency = "0.5"	
				beam.BrickColor = BrickColor.new("Bright yellow")
	if dist >= 60 then
	beam.Size = Vector3.new( 0.042, 0.001)
	else
    beam.Size = Vector3.new(.1,.1,dist)
    end
	beam.CFrame = CFrame.new(script.Parent.RPart.CFrame.p, position) * CFrame.new(0,0,-beam.Size.Z/2)
	game.Debris:AddItem(beam,10)
	local fx = script.Parent.FX:Clone()
	fx.Dist.Value = dist
	fx.Parent = beam
	fx.Disabled = false
	beam.Parent = workspace
	script.Parent.RPart.FlashGui.Label.Rotation = math.random(1, 360)
	script.Parent.RPart.FlashGui.Enabled = true
	script.Parent.RPart.Flash.Enabled = true
	if part ~= nil then
	if part.Parent:FindFirstChild("Humanoid") then
		part.Parent:FindFirstChild("Humanoid"):TakeDamage(script.Parent.EDIT.Damage.Value)
		hit = true
	end
	end
  local hole = Instance.new("Part")
  hole.Size = Vector3.new(.2,.2,.2)
hole.Material = "Plastic"
hole.Anchored = true
hole.BrickColor = BrickColor.new("Black")
hole.Shape = 0
hole.CanCollide = false
hole.Position = position
hole.Transparency = 1	
	game.Debris:AddItem(hole, 1)
	if hit == false then
	hole.Parent = workspace
	end
    wait()
    script.Parent.RPart.FlashGui.Enabled = false
    script.Parent.RPart.Flash.Enabled = false
	end
	elseif ammo.Value <= 0 then
		script.Parent.Handle.NoAmmo:Play()
	end
	wait(script.Parent.EDIT.Firerate.Value)
	-- faster here
	script.Parent.Enabled = true
	end
end)
script.Parent.Reload.OnServerEvent:Connect(function ()
	if reloading == false then
		reloading = true
-- make it slower here
		script.Parent.Handle.Reload:Play()
		wait(script.Parent.EDIT.ReloadTime.Value)
		ammo.Value = maxammo
		reloading = false
-- make it faster here
	end
end)

no time to write but I wrote the “slower here” or there in the script so change it yourself over here
sample code:

Player.Character.Humanoid.WalkSpeed = --your walkspeed
1 Like

It Still Broke The Weapon Again :frowning: Its Fine If You Cant Help

Is there any error stated on the output

Well I Didnt Play It In Studio, Cuz Its a Big Game

Can i see the script u put it in with the changes

If you want to stick with your own gun system, then ignore this, but I would suggest using a gun engine like ACS. It has a lot of features, some of which you can disable, which (as far as I know) makes you slow down when you use a gun (not sure if I read your question correctly, but it slows down the gun and makes the player have realistic movements which may be appropriate for your game).

Well I Dont Want It To Slow Down When Theyre Holding The Gun, I Just Want Them To Slow Down When Theyre Firing And Reloading The Weapon. Also I Dont Use ACS Because Its a Da Hood Game

Edited Script

local maxammo = script.Parent.MaxAmmo.Value
local ammo = script.Parent.Ammo
local reloading = false









local spread = script.Parent.EDIT.Spread.Value






script.Parent.Shoot.OnServerEvent:Connect(function (plr,Target)
	if plr.Character ~= nil and plr.Character.Humanoid.Health >= .1 and script.Parent.Enabled == true and reloading == false then
		script.Parent.Enabled = false
		local hit = false

		if ammo.Value >= 1 then
			ammo.Value = ammo.Value - 1
			script.Parent.Handle.FireSound:Play()
			Player.Character.Humanoid.WalkSpeed = 8
			for i = 1, script.Parent.EDIT.BulletsPerShot.Value do
				local dist = (script.Parent.RPart.Position - Target).Magnitude
				local ray = Ray.new(script.Parent.RPart.CFrame.p, (Target - script.Parent.RPart.CFrame.p + (Vector3.new(math.random(-spread,spread)/1000,math.random(-spread,spread)/1000,math.random(-spread,spread)/1000) * dist)).unit * 2000)
				local part, position = workspace:FindPartOnRay(ray, plr.Character, false, true)
				print(position)
				print(part)
				print(Target)
				local beam = Instance.new("Part")
				beam.Anchored = true
				beam.CanCollide = false
				beam.Material = "Neon"
				beam.Transparency = "0.5"	
				beam.BrickColor = BrickColor.new("Bright yellow")
				if dist >= 60 then
					beam.Size = Vector3.new( 0.042, 0.001)
				else
					beam.Size = Vector3.new(.1,.1,dist)
				end
				beam.CFrame = CFrame.new(script.Parent.RPart.CFrame.p, position) * CFrame.new(0,0,-beam.Size.Z/2)
				game.Debris:AddItem(beam,10)
				local fx = script.Parent.FX:Clone()
				fx.Dist.Value = dist
				fx.Parent = beam
				fx.Disabled = false
				beam.Parent = workspace
				script.Parent.RPart.FlashGui.Label.Rotation = math.random(1, 360)
				script.Parent.RPart.FlashGui.Enabled = true
				script.Parent.RPart.Flash.Enabled = true
				if part ~= nil then
					if part.Parent:FindFirstChild("Humanoid") then
						part.Parent:FindFirstChild("Humanoid"):TakeDamage(script.Parent.EDIT.Damage.Value)
						hit = true
					end
				end
				local hole = Instance.new("Part")
				hole.Size = Vector3.new(.2,.2,.2)
				hole.Material = "Plastic"
				hole.Anchored = true
				hole.BrickColor = BrickColor.new("Black")
				hole.Shape = 0
				hole.CanCollide = false
				hole.Position = position
				hole.Transparency = 1	
				game.Debris:AddItem(hole, 1)
				if hit == false then
					hole.Parent = workspace
				end
				wait()
				script.Parent.RPart.FlashGui.Enabled = false
				script.Parent.RPart.Flash.Enabled = false
			end
		elseif ammo.Value <= 0 then
			script.Parent.Handle.NoAmmo:Play()
		end
		wait(script.Parent.EDIT.Firerate.Value)
		Player.Character.Humanoid.WalkSpeed = 16
		script.Parent.Enabled = true
	end
end)
script.Parent.Reload.OnServerEvent:Connect(function ()
	if reloading == false then
		reloading = true
		Player.Character.Humanoid.WalkSpeed = 8
		script.Parent.Handle.Reload:Play()
		wait(script.Parent.EDIT.ReloadTime.Value)
		ammo.Value = maxammo
		reloading = false
		Player.Character.Humanoid.WalkSpeed = 16
	end
end)

this was meant to be a sample cuz i didn’t had time to adjust the code to your script, nvm I’ll redo it now and u just copy

local maxammo = script.Parent.MaxAmmo.Value
local ammo = script.Parent.Ammo
local reloading = false









local spread = script.Parent.EDIT.Spread.Value






script.Parent.Shoot.OnServerEvent:Connect(function (plr,Target)
	if plr.Character ~= nil and plr.Character.Humanoid.Health >= .1 and script.Parent.Enabled == true and reloading == false then
		script.Parent.Enabled = false
		local hit = false

		if ammo.Value >= 1 then
			ammo.Value = ammo.Value - 1
			script.Parent.Handle.FireSound:Play()
			workspace:FindFirstChild(plr.Name).Humanoid.WalkSpeed = 8
			for i = 1, script.Parent.EDIT.BulletsPerShot.Value do
				local dist = (script.Parent.RPart.Position - Target).Magnitude
				local ray = Ray.new(script.Parent.RPart.CFrame.p, (Target - script.Parent.RPart.CFrame.p + (Vector3.new(math.random(-spread,spread)/1000,math.random(-spread,spread)/1000,math.random(-spread,spread)/1000) * dist)).unit * 2000)
				local part, position = workspace:FindPartOnRay(ray, plr.Character, false, true)
				print(position)
				print(part)
				print(Target)
				local beam = Instance.new("Part")
				beam.Anchored = true
				beam.CanCollide = false
				beam.Material = "Neon"
				beam.Transparency = "0.5"	
				beam.BrickColor = BrickColor.new("Bright yellow")
				if dist >= 60 then
					beam.Size = Vector3.new( 0.042, 0.001)
				else
					beam.Size = Vector3.new(.1,.1,dist)
				end
				beam.CFrame = CFrame.new(script.Parent.RPart.CFrame.p, position) * CFrame.new(0,0,-beam.Size.Z/2)
				game.Debris:AddItem(beam,10)
				local fx = script.Parent.FX:Clone()
				fx.Dist.Value = dist
				fx.Parent = beam
				fx.Disabled = false
				beam.Parent = workspace
				script.Parent.RPart.FlashGui.Label.Rotation = math.random(1, 360)
				script.Parent.RPart.FlashGui.Enabled = true
				script.Parent.RPart.Flash.Enabled = true
				if part ~= nil then
					if part.Parent:FindFirstChild("Humanoid") then
						part.Parent:FindFirstChild("Humanoid"):TakeDamage(script.Parent.EDIT.Damage.Value)
						hit = true
					end
				end
				local hole = Instance.new("Part")
				hole.Size = Vector3.new(.2,.2,.2)
				hole.Material = "Plastic"
				hole.Anchored = true
				hole.BrickColor = BrickColor.new("Black")
				hole.Shape = 0
				hole.CanCollide = false
				hole.Position = position
				hole.Transparency = 1	
				game.Debris:AddItem(hole, 1)
				if hit == false then
					hole.Parent = workspace
				end
				wait()
				script.Parent.RPart.FlashGui.Enabled = false
				script.Parent.RPart.Flash.Enabled = false
			end
		elseif ammo.Value <= 0 then
			script.Parent.Handle.NoAmmo:Play()
		end
		wait(script.Parent.EDIT.Firerate.Value)
	workspace:FindFirstChild(plr.Name).Humanoid.WalkSpeed = 16
		script.Parent.Enabled = true
	end
end)
script.Parent.Reload.OnServerEvent:Connect(function (plr)
	if reloading == false then
		reloading = true
	workspace:FindFirstChild(plr.Name).Humanoid.WalkSpeed = 8
		script.Parent.Handle.Reload:Play()
		wait(script.Parent.EDIT.ReloadTime.Value)
		ammo.Value = maxammo
		reloading = false
		workspace:FindFirstChild(plr.Name).Humanoid.WalkSpeed = 16
	end
end)

client script btw

local held = false
local plr = game.Players.LocalPlayer
script.Parent.Equipped:Connect(function()
	chr = script.Parent.Parent
end)

script.Parent.Activated:Connect(function ()
	held = true
if script.Parent.Enabled == true then
	script.Parent.Enabled = false
	
	
	while held == true do
	if script.Parent.EDIT.BurstShots.Value >= 2 then
	for i = 1, script.Parent.EDIT.BurstShots.Value do
	script.Parent.Shoot:FireServer(chr.Humanoid.TargetPoint)
	fire:Play()
	wait(script.Parent.EDIT.BurstRate.Value)
	
    
	end
	else
	fire:Play()
	script.Parent.Shoot:FireServer(chr.Humanoid.TargetPoint)
	end
	if script.Parent.EDIT.Auto.Value == false then
		held = false
	end
	wait(script.Parent.EDIT.Firerate.Value + .1)
	end
	if script.Parent.EDIT.Auto.Value == false then
		wait(script.Parent.EDIT.Firerate.Value + .1)
	end
	script.Parent.Enabled = true
end
end)

script.Parent.Deactivated:Connect(function()
	held = false
	
end)
script.Parent.Equipped:Connect(function()
	wait()
	holdanim = chr.Humanoid:LoadAnimation(script.Parent.HoldAnim)
	holdanim:Play()
	rel = chr.Humanoid:LoadAnimation(script.Parent.ReloadAnim)
	fire = chr.Humanoid:LoadAnimation(script.Parent.FireAnim)
end)
script.Parent.Unequipped:Connect(function()
	held = false
	holdanim:Stop()
	fire:Stop()
	rel:Stop()
end)
game.Players.LocalPlayer:GetMouse().KeyDown:connect(function(KeyPressed)
	if KeyPressed == "r" and script.Parent.Parent == plr.Character then 
		script.Parent.Reload:FireServer(plr)
		
		rel:Play()
	end
end)
1 Like

Are these even your own scripts? I’m going to guess they aren’t.

I Forgot About That, Let Me Check It Real Quick

Some Parts Are Taken From a Free Model Cuz I Dont Know Much About Scripting, I Know How To Script But Not That Advanced Of a Scripter. [Im Trying To Study Some Parts About Scripting, I Already Know Alot But Still Not Advanced]

Thank You So Much It Actually Works

1 Like