Pvp script problem

Hey! So i’m making a pvp script and found a problem, when I click (or attack) my other friend cant.
I don’t know what’s happening nor do I know how to fix this.
Here is the code.

Variables:

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)	
		local debounce = false
		local animation = script.Right
		local animation2 = script.Left
		local combo = 1

(keep in mind that I put it under character added since this is a server script)

event:

Remote.OnServerEvent:Connect(function(plr, weapon)
if debounce == false then
	debounce = true

Now, i think this is where the problem rises, the part where I do the debounce, I’m not sure why this happens and I’m also not sure on how to fix this.
Thanks!

2 Likes

Can we see the full script, this makes no sense to me.

1 Like

So I’m not sure because you’ve barely any code that shows us the problem and also can you show the output of the error? I’ve a minor theory that maybe you’re coding the PVP system under the PlayerAdded function, this is a big no no!

Reason:

When you use the PlayerAdded function each player that joins the game overwrites the previous player that joined the game, thus making the parameter “player” the new player that joined. This may be the problem however I need to see more of the code to actually catch grasp of what the problem specifically is.

1 Like
Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)	
		local debounce = false
		local animation = script.Right
		local animation2 = script.Left
		local combo = 1
		SwordRemote.OnServerEvent:Connect(function(plr, weapon)
			local character = plr.Character
			local Params = RaycastParams.new()
			Params.FilterDescendantsInstances = {character}
			Params.FilterType = Enum.RaycastFilterType.Blacklist
			
			local hitbox = RaycastHitbox.new(weapon)
			hitbox.RaycastParams = Params
			hitbox.Visualizer = true
			
			if debounce == false then
				debounce = true
				local animator = character:WaitForChild("Humanoid"):WaitForChild("Animator")
				local animTrack
				if combo == 1 then
					animTrack = animator:LoadAnimation(animation)
					combo +=1
				else
					if combo == 2 then
						animTrack = animator:LoadAnimation(animation2)
						combo = 1
					end
				end
				hitbox:HitStart()
				animTrack:Play(-1)
				wait(1)
				hitbox.OnHit:Connect(function(hit, humanoid)
					humanoid:TakeDamage(50)
					kb(hit, character)
					
					local Tagged = Instance.new("ObjectValue")
					Tagged.Name = "damageCredit"
					Tagged.Value = plr
					Tagged.Parent = humanoid
				end)
				animTrack:Stop()
				hitbox:HitStop()
				debounce = false
			end
		end)
	end)
end)

Thats pretty much the full script :smiley:

1 Like

There is no variable called “SwordRemote”.

1 Like

I didn’t include the variables since i didn’t think it was that important but i guess i was wrong:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Storage = ServerStorage:WaitForChild("Storage")
local Modules = Storage:WaitForChild("Modules")
local RaycastHitbox = require(Modules:WaitForChild("RaycastHitboxV4"))
local SwordRemote = ReplicatedStorage:WaitForChild("SwordRemote")
1 Like

kb is just a function that gives knock back, i dont think it has errors/ it has nothing to do with this.

I think you need to have the SwordRemote.OnServerEvent:Connect(function(plr, weapon) outside of the CharacterAdded and PlayerAdded fun.

1 Like

But then where do i put the “client sided” variables? I cant put it in the remote nor the player added.
“Client sided” variables as in:
Debounce, combo ect.

1 Like

Make a table that you can update the variables in.

1 Like

You see, its not that. I need it to be local to the player. If i just put it in the front, when a player sets the debounce to true, then the other player’s debounce is also true.

Your debounce system is currently server-wide, in other words when one client is debounced all clients are, consider using a debounce table instead, here’s an example of the former.

local players = game:GetService("Players")

local debounce = false

players.PlayerAdded:Connect(function(player)
	if debounce then return end
	debounce = true
	print("Hello world!")
	task.wait(10)
	debounce = false
end)

This debounce system makes use of a single state variable which is shared by each player (client), so when one client activates the debounce the debounce becomes activated for every client. Here’s an example of the latter.

local players = game:GetService("Players")

local debounce = {}

players.PlayerAdded:Connect(function(player)
	if debounce[player] then return end
	debounce[player] = true
	print("Hello world!")
	task.wait(10)
	debounce[player] = nil
end)

players.PlayerRemoving:Connect(function(player)
	debounce[player] = nil
end)

Now the debounce system is unique to each player (client) and when one client activates the debounce it becomes activated for just that particular client. With this implementation you can remove leaving players from the table instantly (as it’s faster than waiting for the debounce cool down to expire).

2 Likes