Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

FE Fist combat system issues, both players take damage?

Hi so I'm currently working on a basic fist combat system, I've gotten pretty far to the point where the player can swing and damage with their fists, however, there's an issue within my server script.

I used two variables in my serverscript to detect when the arms should deal damage.

leftDamage = false
rightDamage = false

However, when a player swings with their left arm for example, yes their left arm will deal damage, but so will everybody in the games.

I'm just going to include all the code here, sorry if what I've included is unnecessary

local repStorage = game:GetService("ReplicatedStorage")
local remoteEvent = repStorage:WaitForChild("Swing")
Players = game:GetService("Players")
leftDamage = false
rightDamage = false
char = script.Parent.Parent
char:WaitForChild("Humanoid")
leftarm = char:WaitForChild("Left Arm")
rightarm = char:WaitForChild("Right Arm")



remoteEvent.OnServerEvent:connect(function(player, passthrough)
local char = player.Character
	if passthrough == "L" then
	leftDamage = true
	elseif passthrough == "R" then
		rightDamage = true
	else
	end
end)



leftarm.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
			if leftDamage == true then hit.Parent.Humanoid:TakeDamage(10)
				leftDamage = false
				script.Parent.Sound:Play()
		else
			
		end
		end
end)



rightarm.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
			if rightDamage == true then hit.Parent.Humanoid:TakeDamage(10)
				rightDamage = false
				script.Parent.Sound:Play()
		else
			
		end
		end
end)

1 comment

0

I have a solution im just writing it out

3 answers

If I am able to interpret this correctly, you have a server script in each character.

When the remote event is fired, your script doesn't check if the player who is firing it owns the character model. As a result, when it is fired, all server scripts with this code will set rightDamage or leftDamage to true, indiscriminate of who fired the event originally.

By adding something like if player.Character == char to the remote event's associated function, then you could prevent this from happening.

2 comments

0

im not sure where im putting this but i havent had any luck with it so far

0

The remote event on line 13 fires for all versions of the serverscript, in every character. You need to make sure that its only setting the damage arm to true for the character who actually swung, so you need to include a logic statement.

The solution is that the script checks if the name of the player who was hit was equivalent to the player attacking

local repStorage = game:GetService("ReplicatedStorage")
local remoteEvent = repStorage:WaitForChild("Swing")
Players = game:GetService("Players")
leftDamage = false
rightDamage = false
char = script.Parent.Parent
char:WaitForChild("Humanoid")
leftarm = char:WaitForChild("Left Arm")
rightarm = char:WaitForChild("Right Arm")



remoteEvent.OnServerEvent:connect(function(player, passthrough)
local char = player.Character
    if passthrough == "L" then
    leftDamage = true
    elseif passthrough == "R" then
        rightDamage = true
    else
    end
end)



leftarm.Touched:connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
       if hit.Parent.Name ~= player.name then
            and leftDamage == true then hit.Parent.Humanoid:TakeDamage(10)
                leftDamage = false
                script.Parent.Sound:Play()
        else

        end
        end
end)



rightarm.Touched:connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
       if hit.Parent.Name ~= player.name then
            and rightDamage == true then hit.Parent.Humanoid:TakeDamage(10)
                rightDamage = false
                script.Parent.Sound:Play()
        else

        end
        end
end)

3 comments

0

This should work i think

0

no luck

0

OOF

New script, Put this in the tool directly and not in handles or parts.

local repStorage = game:GetService("ReplicatedStorage")
local remoteEvent = repStorage:WaitForChild("Swing")
Players = game:GetService("Players")
leftDamage = false
rightDamage = false
char = script.Parent.Parent
char:WaitForChild("Humanoid")
leftarm = char:WaitForChild("Left Arm")
rightarm = char:WaitForChild("Right Arm")



remoteEvent.OnServerEvent:connect(function(player, passthrough)
local char = player.Character
    if passthrough == "L" then
    leftDamage = true
    elseif passthrough == "R" then
        rightDamage = true
    else
    end
end)



leftarm.Touched:connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") and hit.Parent ~= script.Parent.Parent then
            if leftDamage == true then hit.Parent.Humanoid:TakeDamage(10)
                leftDamage = false
                script.Parent.Sound:Play()
        else

        end
        end
end)



rightarm.Touched:connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") and hit.Parent ~= script.Parent.Parent then
            if rightDamage == true then hit.Parent.Humanoid:TakeDamage(10)
                rightDamage = false
                script.Parent.Sound:Play()
        else

        end
        end
end)

2 comments

0

It's in startercharacterscripts but I'll try it anyways

0

sadly not sorry

This discussion is preserved as it was. New answers, comments, and votes are closed.