How would I make my stun script better?

so this is my script for a barrage but my problem is the stun inside of it is really bad [as seen in the video]: Streamable

So that is the script (the stun stuff is inside of the .touched function) :

local RS = game:GetService("ReplicatedStorage")
local tweenService = game:GetService("TweenService")
local RagodllModule = require(RS.Modules.Ragdoll)
local PlayersHit = {}
local PlayersHit2 = {}
local activated = false
local RightHit = Instance.new("Animation")
RightHit.AnimationId = "rbxassetid://13474679772"
local LeftHit = Instance.new("Animation")
LeftHit.AnimationId = "rbxassetid://13474685794"
local StunnedGui = RS.Effects:WaitForChild("Stunned")


--Settings--
local damage = 1 --how much damage it deals
local pushForce = 1.5 --knockback
--Settings--

function keepSpeed(plr)
	while activated == true do
		plr.Character.Humanoid.WalkSpeed = 5
		task.wait()
	end
end

function HitboxFunction(hitbox)
	while activated == true do
		hitbox.CanTouch = true
		hitbox.Transparency = 1
		task.wait(0.2)
		hitbox.CanTouch = false
		hitbox.Transparency = 1
	end
end

RS.Events.Barrage.OnServerEvent:Connect(function(plr)
	
	plr.Character.Humanoid.WalkSpeed = 5
	activated = true
	
	RS.Events.Barrage:FireClient(plr)
	
	coroutine.wrap(keepSpeed)(plr)
	
	local PunchSwingSound = script["Punch Swing "]:Clone()
	PunchSwingSound.Parent = plr.Character.HumanoidRootPart
	PunchSwingSound:Play()
	game:GetService("Debris"):AddItem(PunchSwingSound,4)
	
	local Trail = RS.Effects:WaitForChild("Trail"):Clone()
	Trail.Parent = plr.Character.HumanoidRootPart
	Trail.Attachment0 = plr.Character["Left Arm"].LeftShoulderAttachment
	Trail.Attachment1 = plr.Character["Left Arm"].LeftGripAttachment
	
	local Trail2 = RS.Effects:WaitForChild("Trail"):Clone()
	Trail2.Parent = plr.Character.HumanoidRootPart
	Trail2.Attachment0 = plr.Character["Right Arm"].RightShoulderAttachment
	Trail2.Attachment1 = plr.Character["Right Arm"].RightGripAttachment
	
	local plrhighlight = Instance.new("Highlight")
	plrhighlight.Parent = plr.Character
	plrhighlight.DepthMode = Enum.HighlightDepthMode.Occluded
	plrhighlight.FillTransparency = 0
	plrhighlight.FillColor = Color3.fromRGB(255, 255, 255)
	plrhighlight.OutlineTransparency = 1
	game:GetService("TweenService"):Create(plrhighlight, TweenInfo.new(3, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, true), {FillTransparency = 1}):Play()
	game.Debris:AddItem(plrhighlight, 3)
	
	local hitbox = Instance.new("Part", plr.Character)
	hitbox.Anchored = false
	hitbox.CanCollide = false
	hitbox.Size = plr.Character.HumanoidRootPart.Size * 2
	hitbox.CFrame = plr.Character.HumanoidRootPart.CFrame * CFrame.new(0, 0, -2.5)
	hitbox.Color = Color3.fromRGB(255, 0, 0)
	hitbox.Transparency = 1

	local hitboxWeld = Instance.new("ManualWeld")
	hitboxWeld.Name = "HitboxWeld"
	hitboxWeld.Part0 = hitbox
	hitboxWeld.Part1 = plr.Character.HumanoidRootPart
	hitboxWeld.C0 = hitbox.CFrame:ToObjectSpace(plr.Character.HumanoidRootPart.CFrame)
	hitboxWeld.Parent = hitbox
	
	coroutine.wrap(HitboxFunction)(hitbox)
	
	
	hitbox.Touched:Connect(function(hit)
		if  hit.Parent:FindFirstChild("Humanoid") and activated == true and PlayersHit[hit.Parent] == nil then	
			if hit.Parent.Values.Iframes.Value == false and hit.Parent ~= plr.Character then
			PlayersHit[hit.Parent] = true
			local enemy = hit.Parent
			local enemyhum = enemy:WaitForChild("Humanoid")
			
			enemyhum.WalkSpeed = 1
				enemyhum.JumpHeight = 1
			
			local ui = StunnedGui:Clone()
			ui.Parent = hit.Parent.Head
			game.Debris:AddItem(ui, .2)
			
			hit.Parent.Values.Stunned.Value = true

			enemy:WaitForChild("Humanoid"):TakeDamage(damage)

			enemy.HumanoidRootPart.CFrame = CFrame.lookAt(hit.Parent.HumanoidRootPart.Position, plr.Character.HumanoidRootPart.Position)
			
			local track1 = enemyhum:LoadAnimation(RightHit)
			track1:Play()
			
			local BloodEffect = RS.Effects:WaitForChild("Blood").Attachment:Clone()
			BloodEffect.Parent = enemy.HumanoidRootPart
			game:GetService("Debris"):AddItem(BloodEffect,1)
			
			local HitEffect = RS.Effects:WaitForChild("Hit").Attachment:Clone()
			HitEffect.Parent = enemy.HumanoidRootPart
			HitEffect.ParticleEmitter:Emit(10)
			game:GetService("Debris"):AddItem(HitEffect,1)
			
			local PunchSound = script.Punch:Clone()
			PunchSound.Parent = plr.Character.HumanoidRootPart
			PunchSound:Play()
			game:GetService("Debris"):AddItem(PunchSound,.6)

			local vel = Instance.new("BodyVelocity", hit.Parent.HumanoidRootPart)
			vel.MaxForce = Vector3.new(1,1,1) * 1000000;
			vel.Parent = hit.Parent.HumanoidRootPart
			vel.Velocity = Vector3.new(1,1,1) * plr.Character:WaitForChild("HumanoidRootPart").CFrame.LookVector * pushForce
			vel.Name  =  "SmallMoveVel"
			game.Debris:AddItem(vel,0.4) 


			local highlight = Instance.new("Highlight")
			highlight.Parent = enemy
			highlight.DepthMode = Enum.HighlightDepthMode.Occluded
			highlight.FillTransparency = 0
			highlight.OutlineTransparency = 1
			game:GetService("TweenService"):Create(highlight, TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, true), {FillTransparency = 1}):Play()
			game.Debris:AddItem(highlight, 1)
			
			if plr:WaitForChild("RageValues").Ultimate.Value == false then
				plr:WaitForChild("RageValues").RageValue.Value += damage / 2			
			end
			
				local EnemyScript = script.EnemyScript:Clone()
				EnemyScript.Parent = enemy
				EnemyScript.Disabled = false
				game.Debris:AddItem(EnemyScript,1)
			
			task.wait(.2)
			PlayersHit[hit.Parent] = nil
			task.wait(.3)
				if enemy.Values.Stunned.Value == true then
					enemyhum.WalkSpeed = 16
					enemyhum.JumpHeight = 7.2
					hit.Parent.Values.Stunned.Value = false
				end
			end
		end
	end)
	
	hitbox.Touched:Connect(function(hit)
		if  hit.Parent:FindFirstChild("Humanoid") and activated == true and PlayersHit2[hit.Parent] == nil then	
			if hit.Parent.Values.Iframes.Value == false and hit.Parent ~= plr.Character then
				PlayersHit2[hit.Parent] = true
			local enemy = hit.Parent
			local enemyhum = enemy:WaitForChild("Humanoid")
			
			enemyhum.WalkSpeed = 1
			enemyhum.JumpHeight = 1
			
			local ui = StunnedGui:Clone()
			ui.Parent = hit.Parent.Head
			game.Debris:AddItem(ui, .2)
			
			hit.Parent.Values.Stunned.Value = true

			enemy:WaitForChild("Humanoid"):TakeDamage(damage)

			enemy.HumanoidRootPart.CFrame = CFrame.lookAt(hit.Parent.HumanoidRootPart.Position, plr.Character.HumanoidRootPart.Position)
			
			local track2 = enemyhum:LoadAnimation(LeftHit)
			track2:Play()
			
			local BloodEffect = RS.Effects:WaitForChild("Blood").Attachment:Clone()
			BloodEffect.Parent = enemy.HumanoidRootPart
			game:GetService("Debris"):AddItem(BloodEffect,1)
			
			local HitEffect = RS.Effects:WaitForChild("Hit").Attachment:Clone()
			HitEffect.Parent = enemy.HumanoidRootPart
			HitEffect.ParticleEmitter:Emit(10)
			game:GetService("Debris"):AddItem(HitEffect,1)
			
			local PunchSound = script.Punch:Clone()
			PunchSound.Parent = plr.Character.HumanoidRootPart
			PunchSound:Play()
			game:GetService("Debris"):AddItem(PunchSound,.6)

			local vel = Instance.new("BodyVelocity", hit.Parent.HumanoidRootPart)
			vel.MaxForce = Vector3.new(1,1,1) * 1000000;
			vel.Parent = hit.Parent.HumanoidRootPart
			vel.Velocity = Vector3.new(1,1,1) * plr.Character:WaitForChild("HumanoidRootPart").CFrame.LookVector * pushForce
			vel.Name  =  "SmallMoveVel"
			game.Debris:AddItem(vel,0.4) 


			local highlight = Instance.new("Highlight")
			highlight.Parent = enemy
			highlight.DepthMode = Enum.HighlightDepthMode.Occluded
			highlight.FillTransparency = 0
			highlight.OutlineTransparency = 1
			game:GetService("TweenService"):Create(highlight, TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, true), {FillTransparency = 1}):Play()
			game.Debris:AddItem(highlight, 1)
			
			if plr:WaitForChild("RageValues").Ultimate.Value == false then
				plr:WaitForChild("RageValues").RageValue.Value += damage / 2			
				end
				
				local EnemyScript = script.EnemyScript:Clone()
				EnemyScript.Parent = enemy
				EnemyScript.Disabled = false
				game.Debris:AddItem(EnemyScript,1)

			task.wait(.2)
				PlayersHit2[hit.Parent] = nil
			task.wait(.3)
				if enemy.Values.Stunned.Value == true then
					enemyhum.WalkSpeed = 16
					enemyhum.JumpHeight = 7.2
					hit.Parent.Values.Stunned.Value = false
				end
			end
		end
	end)
	
	task.wait(3)
	
	hitbox:Destroy()
	
	PunchSwingSound:Stop()
	
	plr.Character.Humanoid.WalkSpeed = 16
	activated = false
	
	Trail.Enabled = false
	Trail2.Enabled = false
	
	task.wait(1)
	
	Trail:Destroy()
	Trail2:Destroy()
	
end)


Does someone know how to improve that?

1 Like

A lot of Issues,

You can Simplify This piece of code here, Instead of writing Vector3.new(1,1,1), you can just put Vector3.one, as for 1000000, you can put 1e6, which the 6 would be 6 zero’s

Using Variables here would probably imporve the Look, and maybe even the speed of it.

These are not very good uses of while loops and will slow down your game, use RunService Instead.

Use a Variable next time, speeds things up
You should also make sure to delete the Tween when its finished, because overtime it can probably slow you game as it creates an Instance everytime its Created.

Example:

local TweenService = game:GetService("TweenService")
local Debris = game:GetService("Debris")
Debris:AddItem(...)

local Tween = TweenService:Create(...)
Tween:Play()
Debris:AddItem(Tween, ...)

You can only have up to 32 Highlights Rendered at once, and this code would have surpass that limit, so I recommend checking if the NPC even has a Highlight in the first place before you create one each time.

3 Likes

I used some of your improvements and it is definitely better, but I am not sure how to use RunService. Could you tell me how to use it or give me an example?

(if I don’t reply I am currently bussy)

current code:

local RS = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local Debris = game:GetService("Debris")
local RagodllModule = require(RS.Modules.Ragdoll)
local PlayersHit = {}
local PlayersHit2 = {}
local activated = false
local RightHit = Instance.new("Animation")
RightHit.AnimationId = "rbxassetid://13474679772"
local LeftHit = Instance.new("Animation")
LeftHit.AnimationId = "rbxassetid://13474685794"
local StunnedGui = RS.Effects:WaitForChild("Stunned")

local TrailEffect = RS:WaitForChild("Effects"):WaitForChild("Trail")


--Settings--
local damage = 1 --how much damage it deals
local pushForce = 1.5 --knockback
--Settings--

function keepSpeed(plr)
	while activated == true do
		plr.Character.Humanoid.WalkSpeed = 5
		task.wait()
	end
end

function HitboxFunction(hitbox)
	while activated == true do
		hitbox.CanTouch = true
		hitbox.Transparency = 1
		task.wait(0.2)
		hitbox.CanTouch = false
		hitbox.Transparency = 1
	end
end

RS.Events.Barrage.OnServerEvent:Connect(function(plr)
	
	local char = plr.Character
	
	char.Humanoid.WalkSpeed = 5
	activated = true
	
	RS.Events.Barrage:FireClient(plr)
	
	coroutine.wrap(keepSpeed)(plr)
	
	local PunchSwingSound = script["Punch Swing "]:Clone()
	PunchSwingSound.Parent = char.HumanoidRootPart
	PunchSwingSound:Play()
	Debris:AddItem(PunchSwingSound,4)
	
	local Trail = TrailEffect:Clone()
	Trail.Parent = char.HumanoidRootPart
	Trail.Attachment0 = char["Left Arm"].LeftShoulderAttachment
	Trail.Attachment1 = char["Left Arm"].LeftGripAttachment
	
	local Trail2 = TrailEffect:Clone()
	Trail2.Parent = char.HumanoidRootPart
	Trail2.Attachment0 = char["Right Arm"].RightShoulderAttachment
	Trail2.Attachment1 = char["Right Arm"].RightGripAttachment
	
	local plrhighlight = Instance.new("Highlight")
	plrhighlight.Parent = char
	plrhighlight.DepthMode = Enum.HighlightDepthMode.Occluded
	plrhighlight.FillTransparency = 0
	plrhighlight.FillColor = Color3.fromRGB(255, 255, 255)
	plrhighlight.OutlineTransparency = 1
	game:GetService("TweenService"):Create(plrhighlight, TweenInfo.new(3, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, true), {FillTransparency = 1}):Play()
	Debris:AddItem(plrhighlight, 3)
	
	local hitbox = Instance.new("Part", char)
	hitbox.Anchored = false
	hitbox.CanCollide = false
	hitbox.Size = char.HumanoidRootPart.Size * 2
	hitbox.CFrame = char.HumanoidRootPart.CFrame * CFrame.new(0, 0, -2.5)
	hitbox.Color = Color3.fromRGB(255, 0, 0)
	hitbox.Transparency = 1

	local hitboxWeld = Instance.new("ManualWeld")
	hitboxWeld.Name = "HitboxWeld"
	hitboxWeld.Part0 = hitbox
	hitboxWeld.Part1 = char.HumanoidRootPart
	hitboxWeld.C0 = hitbox.CFrame:ToObjectSpace(char.HumanoidRootPart.CFrame)
	hitboxWeld.Parent = hitbox
	
	coroutine.wrap(HitboxFunction)(hitbox)
	
	
	hitbox.Touched:Connect(function(hit)
		if  hit.Parent:FindFirstChild("Humanoid") and activated == true and PlayersHit[hit.Parent] == nil then	
			if hit.Parent.Values.Iframes.Value == false and hit.Parent ~= char then
				PlayersHit[hit.Parent] = true
				local enemy = hit.Parent
				local enemyhum = enemy:WaitForChild("Humanoid")
			
				enemyhum.WalkSpeed = 1
				enemyhum.JumpHeight = 1
			
				local ui = StunnedGui:Clone()
				ui.Parent = hit.Parent.Head
				Debris:AddItem(ui, .2)
			
				hit.Parent.Values.Stunned.Value = true

				enemy:WaitForChild("Humanoid"):TakeDamage(damage)

				enemy.HumanoidRootPart.CFrame = CFrame.lookAt(hit.Parent.HumanoidRootPart.Position, char.HumanoidRootPart.Position)
			
				local track1 = enemyhum:LoadAnimation(RightHit)
				track1:Play()
			
				local BloodEffect = RS.Effects:WaitForChild("Blood").Attachment:Clone()
				BloodEffect.Parent = enemy.HumanoidRootPart
				Debris:AddItem(BloodEffect,1)
			
				local HitEffect = RS.Effects:WaitForChild("Hit").Attachment:Clone()
				HitEffect.Parent = enemy.HumanoidRootPart
				HitEffect.ParticleEmitter:Emit(10)
				Debris:AddItem(HitEffect,1)
			
				local PunchSound = script.Punch:Clone()
				PunchSound.Parent = char.HumanoidRootPart
				PunchSound:Play()
				Debris:AddItem(PunchSound,.6)

				local vel = Instance.new("BodyVelocity", hit.Parent.HumanoidRootPart)
				vel.MaxForce = Vector3.one * 1e6
				vel.Parent = hit.Parent.HumanoidRootPart
				vel.Velocity = Vector3.new(1,1,1) * char:WaitForChild("HumanoidRootPart").CFrame.LookVector * pushForce
				vel.Name  =  "SmallMoveVel"
				Debris:AddItem(vel,0.4) 


				if enemy:FindFirstChild("BarrageHighlight") then

				else
					local highlight = Instance.new("Highlight")
					highlight.Parent = enemy
					highlight.Name = "BarrageHighlight"
					highlight.DepthMode = Enum.HighlightDepthMode.Occluded
					highlight.FillTransparency = 0
					highlight.OutlineTransparency = 1
					local HighlightTween1 = game:GetService("TweenService"):Create(highlight, TweenInfo.new(.5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, true), {FillTransparency = 1}):Play()
					Debris:AddItem(highlight, .5)
					Debris:AddItem(HighlightTween1, .5)
				end
			
			if plr:WaitForChild("RageValues").Ultimate.Value == false then
				plr:WaitForChild("RageValues").RageValue.Value += damage / 2			
			end
			
				local EnemyScript = script.EnemyScript:Clone()
				EnemyScript.Parent = enemy
				EnemyScript.Disabled = false
				Debris:AddItem(EnemyScript,1)
			
			task.wait(.2)
			PlayersHit[hit.Parent] = nil
			task.wait(.3)
				if enemy.Values.Stunned.Value == true then
					enemyhum.WalkSpeed = 16
					enemyhum.JumpHeight = 7.2
					hit.Parent.Values.Stunned.Value = false
				end
			end
		end
	end)
	
	hitbox.Touched:Connect(function(hit)
		if  hit.Parent:FindFirstChild("Humanoid") and activated == true and PlayersHit2[hit.Parent] == nil then	
			if hit.Parent.Values.Iframes.Value == false and hit.Parent ~= char then
				PlayersHit2[hit.Parent] = true
				local enemy = hit.Parent
				local enemyhum = enemy:WaitForChild("Humanoid")
			
				enemyhum.WalkSpeed = 1
				enemyhum.JumpHeight = 1
			
				local ui = StunnedGui:Clone()
				ui.Parent = hit.Parent.Head
				Debris:AddItem(ui, .2)
			
				hit.Parent.Values.Stunned.Value = true

				enemy:WaitForChild("Humanoid"):TakeDamage(damage)

				enemy.HumanoidRootPart.CFrame = CFrame.lookAt(hit.Parent.HumanoidRootPart.Position, char.HumanoidRootPart.Position)
			
				local track2 = enemyhum:LoadAnimation(LeftHit)
				track2:Play()
			
				local BloodEffect = RS.Effects:WaitForChild("Blood").Attachment:Clone()
				BloodEffect.Parent = enemy.HumanoidRootPart
				Debris:AddItem(BloodEffect,1)
			
				local HitEffect = RS.Effects:WaitForChild("Hit").Attachment:Clone()
				HitEffect.Parent = enemy.HumanoidRootPart
				HitEffect.ParticleEmitter:Emit(10)
				Debris:AddItem(HitEffect,1)
			
				local PunchSound = script.Punch:Clone()
				PunchSound.Parent = char.HumanoidRootPart
				PunchSound:Play()
				Debris:AddItem(PunchSound,.6)

				local vel = Instance.new("BodyVelocity", hit.Parent.HumanoidRootPart)
				vel.MaxForce = Vector3.one * 1e6
				vel.Parent = hit.Parent.HumanoidRootPart
				vel.Velocity = Vector3.new(1,1,1) * char:WaitForChild("HumanoidRootPart").CFrame.LookVector * pushForce
				vel.Name  =  "SmallMoveVel"
				Debris:AddItem(vel,0.4) 

				
				if enemy:FindFirstChild("BarrageHighlight") then
					
				else
					local highlight = Instance.new("Highlight")
					highlight.Parent = enemy
					highlight.Name = "BarrageHighlight"
					highlight.DepthMode = Enum.HighlightDepthMode.Occluded
					highlight.FillTransparency = 0
					highlight.OutlineTransparency = 1
					local HighlightTween1 = game:GetService("TweenService"):Create(highlight, TweenInfo.new(.5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, true), {FillTransparency = 1}):Play()
					Debris:AddItem(highlight, .5)
					Debris:AddItem(HighlightTween1, .5)
				end
				
			
				if plr:WaitForChild("RageValues").Ultimate.Value == false then					
					plr:WaitForChild("RageValues").RageValue.Value += damage / 2							
				end
				
				local EnemyScript = script.EnemyScript:Clone()
				EnemyScript.Parent = enemy				
				EnemyScript.Disabled = false
				Debris:AddItem(EnemyScript,1)

				task.wait(.2)				
				PlayersHit2[hit.Parent] = nil				
				task.wait(.3)				
				if enemy.Values.Stunned.Value == true then
					enemyhum.WalkSpeed = 16
					enemyhum.JumpHeight = 7.2
					hit.Parent.Values.Stunned.Value = false
				end
			end
		end
	end)
	
	task.wait(3)
	
	hitbox:Destroy()
	
	PunchSwingSound:Stop()
	
	char.Humanoid.WalkSpeed = 16
	activated = false
	
	Trail.Enabled = false
	Trail2.Enabled = false
	
	task.wait(1)
	
	Trail:Destroy()
	Trail2:Destroy()
	
end)

RunService is typically used to have your code run frame by frame, in the case of the while, you are using task.wait() to yield the code going on, which does the exact thing as RunService.Heartbeat:Wait(), Heartbeat Runs After Physics every Frame, and using this, you can have it so the code runs every frame with this event, it will not yield the code from the Main Thread.

RunService.Heartbeat:Connect(function(dt) -- its essentially one big loop using task.wait()
    if activated then -- equivilent to "if activated == true then"
        -- code
    end
end)

Here is Documentation on that.

1 Like

Thanks all of this really helped to improve the script and I can use this stuff for my other script. I didn’t even know you had to destroy tween!

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