I've been making a combat system and I just discovered that when one press Q (Key for left punch), everyone on the server left punches? What might be the problem? Please help, thanks!
Server Script:
game.Players.PlayerAdded:Connect(function(player)
local player = player
local debounce = false
local leftPunching = false
local rightPunching = false
local leftKicking = false
local rightKicking = false
local LPcooldown = false
local RPcooldown = false
local LKcooldown = false
local RKcooldown = false
local function leftPunch()
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local leftPunch = script.LeftPunch
local leftPunchTrack = humanoid:LoadAnimation(leftPunch)
local leftFist = character:WaitForChild("Left Arm")
if not LPcooldown then
LPcooldown = true
leftPunching = true
leftPunchTrack:Play()
wait(1)
LPcooldown = false
leftPunching = false
end
leftFist.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
if humanoid and leftPunching then
if not debounce then
debounce = true
humanoid:TakeDamage(10)
wait(.5)
debounce = false
end
end
end)
end
local function rightPunch()
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local rightPunch = script.RightPunch
local rightPunchTrack = humanoid:LoadAnimation(rightPunch)
local rightFist = character:WaitForChild("Right Arm")
if not RPcooldown then
RPcooldown = true
rightPunching = true
rightPunchTrack:Play()
wait(1)
RPcooldown = false
rightPunching = false
end
rightFist.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
if humanoid and rightPunching then
if not debounce then
debounce = true
humanoid:TakeDamage(10)
wait(.5)
debounce = false
end
end
end)
end
local function leftKick()
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local leftKick = script.LeftKick
local leftKickTrack = humanoid:LoadAnimation(leftKick)
local leftFoot = character:WaitForChild("Left Leg")
if not LKcooldown then
LKcooldown = true
leftKicking = true
leftKickTrack:Play()
wait(1)
LKcooldown = false
leftKicking = false
end
leftFoot.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
if humanoid and leftKicking then
if not debounce then
debounce = true
humanoid:TakeDamage(20)
wait(.5)
debounce = false
end
end
end)
end
local function rightKick()
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local rightKick = script.RightKick
local rightKickTrack = humanoid:LoadAnimation(rightKick)
local rightFoot = character:WaitForChild("Right Leg")
if not RKcooldown then
RKcooldown = true
rightKicking = true
rightKickTrack:Play()
wait(1)
RKcooldown = false
rightKicking = false
end
rightFoot.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
if humanoid and rightKicking then
if not debounce then
debounce = true
humanoid:TakeDamage(20)
wait(.5)
debounce = false
end
end
end)
end
game.ReplicatedStorage.RemoteLP.OnServerEvent:Connect(leftPunch)
game.ReplicatedStorage.RemoteRP.OnServerEvent:Connect(rightPunch)
game.ReplicatedStorage.RemoteLK.OnServerEvent:Connect(leftKick)
game.ReplicatedStorage.RemoteRK.OnServerEvent:Connect(rightKick)
end)
Client:
local UIS = game:GetService("UserInputService")
UIS.InputBegan:Connect(function(input, gameProcessed)
if input.KeyCode == Enum.KeyCode.Q then
game.ReplicatedStorage.RemoteLP:FireServer()
end
if input.KeyCode == Enum.KeyCode.E then
game.ReplicatedStorage.RemoteRP:FireServer()
end
if input.KeyCode == Enum.KeyCode.Z then
game.ReplicatedStorage.RemoteLK:FireServer()
end
if input.KeyCode == Enum.KeyCode.C then
game.ReplicatedStorage.RemoteRK:FireServer()
end
end)

1 comment
You connected the remotes to e v e r y player with PlayerAdded; remove that tactic, and reprogram the Server. Remotes supply the Client responsible for invocation, so you don't have to worry about tuning to each individual Player.