Slowing Down Players When Firing

Hello! So Today I Am Making a Gun Engine For My Hood/Reallife Game And I Would Like To Ask,
How Do I Make My Gun Engine Slow Down The Player When Firing ?
I’ve Tried Editing It But It Broke The Weapon, Does Anyone Have a Solution To This ?
You Can Try Editing The Code Below :
Handler

local character
local humanoid
local animator
local animate
local animate2
local equipped = false
local filterArray = {}
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

damage = 17

script.Parent.RequestFire.OnServerEvent:Connect(function(plr,targetPosition)
	if script.Parent:FindFirstChild("Handle") and equipped == true then
		if animate2 then
			animate2:Stop()
		end
		animate2 = animator:LoadAnimation(script.Fire)
		animate2:Play()
		
		local spreadPosition = targetPosition + Vector3.new(math.random(-500,500)/1250,math.random(-500,500)/1250,math.random(-500,500)/1250)
		
		local raycastResult = workspace:Raycast(
			(script.Parent.Handle.CFrame * CFrame.new(-1.9,0.11,0)).Position,
			(spreadPosition - (script.Parent.Handle.CFrame * CFrame.new(-1.9,0.11,0)).Position).Unit*500,
			raycastParams)

		if raycastResult then
			local partHit = raycastResult.Instance
			if partHit.Parent:FindFirstChild("Humanoid") then
				if partHit.Parent.Humanoid.Health > 0 then
					if partHit.Name == "Head" then
						partHit.Parent.Humanoid:TakeDamage(damage*2.5)
					else
						partHit.Parent.Humanoid:TakeDamage(damage)
					end
					if partHit.Parent:FindFirstChild("Burn") == nil then
						local burn = script.Burn:Clone()
						burn.Parent = partHit.Parent
						burn.Disabled = false
					else
						partHit.Parent:FindFirstChild("Burn").BurningTime.Value = partHit.Parent:FindFirstChild("Burn").BurningTime.Value + 2
					end
				end
			elseif partHit.Parent:IsA("Accessory") then
				for _, ins in pairs(partHit.Parent.Parent:GetChildren()) do
					if ins:IsA("Accessory") then
						table.insert(filterArray,ins)
					end
				end
				raycastParams.FilterDescendantsInstances = filterArray
			end
		end
		coroutine.wrap(function()
			script.Parent.Handle.PointLight.Enabled = true
			script.Parent.Handle.BillboardGui.Enabled = true
			script.Parent.Rpart.FlashGui.Enabled = true
			wait(.08)
			script.Parent.Handle.PointLight.Enabled = false
			script.Parent.Handle.BillboardGui.Enabled = false
			script.Parent.Rpart.FlashGui.Enabled = false
		end)()
		local trailPart = Instance.new("Part",workspace)
		trailPart.Name = "TrailPart"
		trailPart.Transparency = 1
		trailPart.CanCollide = false
		trailPart.Anchored = true
		local attachment1 = Instance.new("Attachment",trailPart)
		local beam = Instance.new("Beam",trailPart)
		game.Debris:AddItem(attachment1,.075)
		game.Debris:AddItem(trailPart,.075)
		attachment1.WorldPosition = spreadPosition
		beam.Color = ColorSequence.new(Color3.fromRGB(243, 255, 8),Color3.fromRGB(251, 255, 0))
		beam.LightEmission = 1
		beam.LightInfluence = 0
		beam.Transparency = NumberSequence.new(0,1)
		beam.Attachment0 = script.Parent.Handle.BarrelAttachment
		beam.Attachment1 = attachment1
		beam.FaceCamera = true
		beam.Width0 = 0.100
		beam.Width1 = 0.100
		script.Parent.Handle.Fire1.PlaybackSpeed = 1 + math.random(-100,100)/1000
		script.Parent.Handle.Fire1:Play()
		script.Parent.Handle.Fire2.PlaybackSpeed = 1.25 + math.random(-100,100)/250
		script.Parent.Handle.Fire2:Play()
	end
end)
script.Parent.Activated:Connect(function()
	script.Parent.Activated:Connect(function()
		for _, ins in pairs(workspace:GetChildren()) do
			if ins:FindFirstChild("Humanoid") then
				for _, ins2 in pairs(ins:GetChildren()) do
					if ins2:IsA("Accessory") then
						if table.find(filterArray,ins2) == nil then
							table.insert(filterArray,ins2)
						end
					end
				end
			end
		end
		raycastParams.FilterDescendantsInstances = filterArray
	end)
end)
script.Parent.Equipped:Connect(function()
	equipped = true
	character = script.Parent.Parent
	if table.find(filterArray,script.Parent.Parent) == nil then
		table.insert(filterArray,script.Parent.Parent)
	end
	if table.find(filterArray,script.Parent) == nil then
		table.insert(filterArray,script.Parent)
	end
	raycastParams.FilterDescendantsInstances = filterArray
	if character then
		humanoid = character.Humanoid
		if humanoid:FindFirstChild("Animator") == nil then
			animator = Instance.new("Animator",humanoid)
		elseif humanoid:FindFirstChild("Animator") then
			animator = humanoid:FindFirstChild("Animator")
		end
	end
	animate = animator:LoadAnimation(script.Hold)
	animate:Play()
end)
script.Parent.Unequipped:Connect(function()
	equipped = false
	if script.Parent:FindFirstChild("Handle") then
		script.Parent.Handle.PointLight.Enabled = false
		script.Parent.Handle.BillboardGui.Enabled = false
	end
	for _, ins in pairs(script.Parent.Handle:GetChildren()) do
		if ins:IsA("Sound") then
			ins:Stop()
		elseif ins:IsA("PointLight") then
			ins.Enabled = false
		end
	end
	if animate then
		animate:Stop()
	end
	if animate2 then
		animate2:Stop()
	end
end)

LocalHandler

local mouse = game.Players.LocalPlayer:GetMouse()
local equipped = false
local connection
local connection2
local active = false
local debounce = false
local isSemi = true

script.Parent.Equipped:Connect(function()
	equipped = true
	connection = mouse.Button1Down:Connect(function()
		if equipped == true and debounce == false then
			if isSemi == false then
				active = true
				repeat
					if debounce == false then
						debounce = true
						local mousePos = mouse.Hit.Position
						script.Parent.RequestFire:FireServer(mousePos)
					end
					wait(0.08)
					debounce = false
				until active == false
			elseif isSemi == true then
				if debounce == false then
					debounce = true
					local mousePos = mouse.Hit.Position
					script.Parent.RequestFire:FireServer(mousePos)
					wait(0.3) ----------- Change The CoolDown Each Shot
					debounce = false
				end
			end

		end
	end)
	connection2 = mouse.Button1Up:Connect(function()
		if equipped == true then
			active = false
		end
	end)
end)
script.Parent.Unequipped:Connect(function()
	equipped = false
	active = false
	connection:Disconnect()
	connection2:Disconnect()
end)

If You Can Help Please Reply, I Need This For My Game

1 Like

In your local handler make a reference to your humanoid.

On mousebuttondown do Humanoid.WalkSpeed = 12 (normal is 16)

On MousebuttonUp do Humanoid.WalkSpeed = 16

Even tho it is in a local script, changes to humanoid speed are visible from other clients too. The actual WalkSpeed Value will change only for the particular client, but he will be moving slower for other clients too. Alternatively, if you want to hussle too much, you can do the same thing i said on server side with remote events, but i see no point in that, even for anticheat purposes.

Well What Line Do I Put The Code In ? I Tried Everything To Edit It But It Broke The Weapon

If you dont know where to put in that simple statement, you probably just copied the code above from somewhere else. I can tell you exactly where to put it, but there is no point in that, you wont be able to make a full good game by copying. I encourage you to learn scripting, there are lot of free resources, i can share good ones with you if you want.

The Weapon Engine Is Scripted a Few Weeks Ago With a Help From My Friend, I Know How To Code But Not That Advanced.
[Edit : My Friend Isnt Online For Quite a Long Time. So I Cant Ask Him For Some Help]

Could You Teach Me About Coding Stuffs ? I Really Wanna Learn

this is a really good playlist for learning the basics. Even if you know the basics, you should watch this, because there are some good life hacks and things you might not have noticed.

As for becoming advanced, the best way is making your game, using the help of roblox dev hub (after you ve mastered the basics of course).

If any particular questions come up, answers of which you cant found on the internet, feel free to reach out, ill be glad to help.

btw this is not advertisement, there are some other playlists in youtube that are just as good or even better, this is just the one i subjectively liked

Also Is There Any Way I Could Multiply The Ray In The Server Script ?

When firing your gun within the mouse.Button1Down event, during firing the remote event with your mousePos argument, you can pass an additional argument called “IsFiring”; which is a bool that is true, when the OnServerEvent fires from the remote you can check the IsFiring bool and if it is true, lower the player’s humanoid walkspeed to 12, when the mouse.Button1Up event fires, pass the IsFiring bool as false so that when the server picks it up and reads that it’s false, it will return the walkspeed to 16 and since your not going to fire anymore, add a return below setting the walkspeed back to 16

When firing, just have a BoolValue set to true and have a loop that checks if the value is true and if it is, you slow down.

I Really Dont Understand Right Now ;-;
Im a Small Scripter, The Weapon Engine Is Made With The Help Of My Friend Which Isnt Online Rn

But For The Walkspeed Thing, I Dont Think I Really Need It Or Something, I Just Found An Example Code And I Would Try Editing It MySelf, Like @Denz_Noviembre Said You Cant Make a Good Game By Just Copying Other’s Scripts. I Would Still Do Some Hardwork Rather Than Finding Free Models And Such, Im More Of a Builder Rather Than a Scripter

1 Like

Thanks For The Advice @Denz_Noviembre I Finally Figured It Out How :smiley:
But Theres One Issue Tho, After Editing It I Somehow Slow Down Whenever I Hold Down My LeftButton On My Mouse When It’s Semi, It Wasnt Supposed To Do That. Could You Help Me ?

In the line where you lowered the humanoid walk speed add a checking statement

if isSemi ~= true do
    --change the speed
end

So whenever you re firing in Semi it wont slow down

Nvm I Figured It Out, But Thanks For The Help