Issue with Stun system

So i was making a stun system where by the script below continuously checks if player is stun, then runs the command, issue is that the first if statement doesn’t work but the else statement does.

local player = game:GetService("Players").LocalPlayer
local chr = player.Character or player.CharacterAdded:Wait()
local runservice = game:GetService("RunService")
local rs = game:GetService("ReplicatedStorage")

local stunnedevent = rs:WaitForChild("StunnedEvent")


coroutine.resume(coroutine.create(function()
		while true do
			wait()
		if chr:GetAttribute("Stunned") == true then
			print("player is stunned")
			stunnedevent:FireServer()
			task.wait(0.5)
			print("Stunned")
			chr:SetAttribute("Stunned", false)
		else
			wait(3)
			print("not stunned")
		end
		end
end))

try this

chr:SetAttribute("Stunned", nil)

and remove == true.

it still doesn’t work event though the players stunned attribute is on

Try moving chr:SetAttribute("Stunned", false) up at here if chr:GetAttribute("Stunned") == true then but under it, so it’s the first thing that is done when if the attribute is true.

it still doesn’t work, should i use runservice instead maybe?

That’s what I was thinking, I’m not sure why you’re using a loop, it would be a good idea to make it whatever studs the player, into a like event, not a loop, that can be hard on mem and make lag later on.

You could use part:GetAttributeChangedSignal to make it more performant and easier.

Simple change:

local player = game:GetService("Players").LocalPlayer
local chr = player.Character or player.CharacterAdded:Wait()
local runservice = game:GetService("RunService")
local rs = game:GetService("ReplicatedStorage")

local stunnedevent = rs:WaitForChild("StunnedEvent")

local Attribute = "Stunned"

local function AttributeChanged()
	if chr:GetAttribute(Attribute) then
		print("player is stunned")
		stunnedevent:FireServer()
		task.wait(0.5)
		print("Stunned")
		chr:SetAttribute(Attribute, false)
	else	
		task.wait(3)
		print("not stunned")
	end
end

chr:GetAttributeChangedSignal(Attribute):Connect(AttributeChanged)

I see that there should be a small change. (correct me if I am wrong)

local player = game:GetService("Players").LocalPlayer
local chr = player.Character or player.CharacterAdded:Wait()
local runservice = game:GetService("RunService")
local rs = game:GetService("ReplicatedStorage")

local stunnedevent = rs:WaitForChild("StunnedEvent")

local AttributeName = "Stunned"

local function AttributeChanged()
	local Attribute = chr:GetAttribute(AttributeName)

	if Attribute then
		print("player is stunned")
		stunnedevent:FireServer()
		task.wait(0.5)
		print("Stunned")
		chr:SetAttribute(AttributeName, false)
	else	
		task.wait(3)
		print("not stunned")
	end
end

chr:GetAttributeChangedSignal(AttributeName):Connect(AttributeChanged)

Oh yea, I forgot to make different variables. Good thing you caught that

1 Like

Sorry for the late reply, I appreciate the help unfortunately it doesn’t work

Could the issue be with how I’m setting the attribute?

local uis = game:GetService("UserInputService")
local rs = game:GetService("ReplicatedStorage")
local player = game:GetService("Players").LocalPlayer
local chr = player.Character
local DashAttackEvent = rs:WaitForChild("DashAttackEvent")
local hum = chr:WaitForChild("Humanoid")
local stunnedevent = rs:WaitForChild("StunnedEvent")
local ListOfWeapons = {
	"BasicSword",
	"RengokuSword",
	"PlaceHolder1",
}


local module = require(rs:WaitForChild("Modules").CombatSystemModule)
local CombatNum = 1
local debounce = false
local candashattack = true

uis.InputBegan:Connect(function(input,e)
	if e then return end

	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		for i,v in player.Character:GetChildren() do
			if table.find(ListOfWeapons, v.Name) and v:IsA("MeshPart") and debounce == false and chr:GetAttribute("isDashing") == false and chr:GetAttribute("Stunned") == false and chr:GetAttribute("IsBlocking") == false  and chr:GetAttribute("IsAttacking") == false and chr:GetAttribute("Parried") == false then
				debounce = true-- assuming this is the problem, as it may of let other MeshParts through
				module.CombatSystem(player, CombatNum)
				CombatNum += 1

				if CombatNum > 4 then
					CombatNum = 1
				end
				local hitboxCFrame = chr.PrimaryPart.CFrame * CFrame.new(0,0,-2.5)
				local hitboxSize = Vector3.new(5,5,5)
				local damage = 13
				local hitbox = rs.Hitbox:Clone()
				hitbox.Parent = workspace
				hitbox.CFrame = hitboxCFrame
				hitbox.Size = Vector3.new(hitboxSize, hitboxSize, hitboxSize)
				local hitcontent = workspace:GetPartBoundsInBox(hitboxCFrame, hitboxSize)
				local hitlist = {}
				for _,v in pairs(hitcontent) do
					if v.Parent:FindFirstChild("Humanoid") and v.Parent ~= chr and not table.find(hitlist, v.Parent) then
						table.insert(hitlist, v.Parent)
						v.Parent.Humanoid:TakeDamage(damage)
						v.Parent:SetAttribute("Stunned", true)
					end
				end
				----for i,v in pairs(hitlist) do
				----	print(v.Name)
				----	if v:FindFirstChild("Blocking") then
				----		v:FindFirstChild("Blocking"):Destroy()
				----	end
				--end
				
			   task.wait(0.5)
			   debounce = false
				return
			end
			
			if table.find(ListOfWeapons, v.Name) and v:IsA("MeshPart") and  chr:GetAttribute("Stunned") == false and  chr:GetAttribute("isDashing") == true and candashattack == true then
				candashattack = false--
				chr:SetAttribute("IsAttacking", true)
				local dashattackanim = game:GetService("ReplicatedFirst").Animations.DashAttackAim
				local dashattacktrack = hum:LoadAnimation(dashattackanim)
				dashattacktrack:Play()
				dashattacktrack:GetMarkerReachedSignal("SpawnHitBoxEvent"):connect(function()
					CombatNum += 1
					DashAttackEvent:FireServer()

					if CombatNum > 4 then
						CombatNum = 1
					end

					local hitboxCFrame = chr.HumanoidRootPart.CFrame * CFrame.new(0,0,0)
					local hitboxSize = Vector3.new(10,5,20)
					local damage = 18
					local hitbox = rs.Hitbox:Clone()
					hitbox.Parent = workspace
					hitbox.CFrame = hitboxCFrame
					hitbox.Size = Vector3.new(hitboxSize, hitboxSize, hitboxSize)
					local hitcontent = workspace:GetPartBoundsInBox(hitboxCFrame, hitboxSize)
					local hitlist = {}
					for _,v in pairs(hitcontent) do
						if v.Parent:FindFirstChild("Humanoid") and v.Parent ~= chr and not table.find(hitlist, v.Parent) then
							table.insert(hitlist, v.Parent)
							v.Parent.Humanoid:TakeDamage(damage)
							v.Parent:SetAttribute("Stunned", true)
						end
					end
				end)
				task.wait(1)
				chr:SetAttribute("IsAttacking", false)

				
				----for i,v in pairs(hitlist) do
				----	print(v.Name)
				----	if v:FindFirstChild("Blocking") then
				----		v:FindFirstChild("Blocking"):Destroy()
				----	end
				--end

				task.wait(4)
				candashattack = true
				return
			end
		end
	end
end)

edit: sorry in advanced, ik the code is hell to read

Hello there!

If I understood correctly, the attacker sets the attribute stunned on the hitted entity.

If that’s true, then there’s the problem. For the attacker, the entity is stunned, but it does NOT replicate it to entity’s perspective! So the entity doesn’t know it’s Stunned!

To solve that you’ll need to update the attributes on the server trough a remote event.

You can create a localscript

Character:GetAttributeChangedSignal:Connect(function(Attribute)

local Value = Character:GetAttribute(Attribute)

RemoteEvent:FireServer(Attribute, Value)

end)

And a server Script

RemoteEvent.OnClientEvent:Connect(function(player, Attribute, Value)

player.Character:SetAttribute(Attribute, Value)

end)

But that can be easily exploited, so what i recomend to do is to make the server make the hit checks, and the client just ask to make an attack

But if it is a singleplayer game then the exploiters still can abuse this, but it’ll ruin just his own fun.

Hope it helps :wink:

Oh i understand the issue now thanks a lot, also would a module script be exploitable also?

Me personally I’d say you should send a table of the hit characters to the server, do checks like a distance check, and then stun & damage their humanoid on the server.

1 Like