Script seemingly skipping part of if statement when there is more than one person playing

edit: it only happens when theres more than one person playing; changed the title

hi, so when i tried to test my game, i noticed that the behavior of my scripts in-game was different than the behavior of my scripts in studio. i tested my game with two players in-game, (not in studio however) and whenever a player tried to punch anything, even just an inanimate part, the player would fall down and take damage. the punching otherwise works fine; if you punch a player, they fall down and take damage too, but taking damage and falling every time you punch something wouldnt be very good game design. in studio however, if you punch anything, you dont fall down nor take damage, as expected. now, i havent tested team test in studio as i cant really do that, but it may be related to the amount of players.

important things to keep in mind before reading the script:

im a beginner scripter. the solution is probably pretty obvious and my code is very messy

(username)“Clone” is a ragdoll that doesnt collide with the player and is controlled by the character, with the character invisible so that it appears as if the ragdoll is being directly controlled by the player.

script (local script in startercharacterscripts):

--useful variables
local player = game.Players.LocalPlayer
local character = player.Character
local playerHumanoid = character:WaitForChild("Humanoid")

--punching
local punchEquipAnimation = game.ReplicatedStorage.Animations.punchEquipAnimation
local leftPunchAnimation = punchEquipAnimation.Parent.LeftPunchAnimation
local rightPunchAnimation = leftPunchAnimation.Parent.RightPunchAnimation
local canPunch = true
local canBePunched = true
local timesClicked = 0
local connection
local connection2
local connection3

task.wait(1)

for _, workspaceObject in pairs(workspace:GetChildren()) do
	if workspaceObject:IsA("Model") and workspaceObject.Name == character.Name.."Clone" then
		local humanoid = workspaceObject:WaitForChild("Humanoid")
		
		player.Backpack.punch.Equipped:Connect(function()
			if true then
				for _, displayCharacterObject in pairs(workspaceObject:GetDescendants()) do
					if displayCharacterObject:IsA("Motor6D") then
						if displayCharacterObject.Name == "Left Shoulder" or displayCharacterObject.Name == "Right Shoulder" then
							game.ReplicatedStorage.Events.PunchEvent:FireServer(displayCharacterObject)
						end
					end
				end

				game.ReplicatedStorage.Events.punchEquipAnimationEvent:FireServer()

				connection = game:GetService("UserInputService").InputBegan:Connect(function(input, gameProcEvent)
					if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
						if gameProcEvent then return end

						if canPunch == true then
							canPunch = false

							timesClicked = timesClicked + 1

							if timesClicked == 1 or timesClicked == 3 then
								timesClicked = 1

								game.ReplicatedStorage.Events.punchActivateEvent:FireServer()
								game.ReplicatedStorage.Events.LeftPunchAnimationEvent:FireServer()
							end
							if timesClicked == 2 then
								game.ReplicatedStorage.Events.punchActivateEvent:FireServer()
								game.ReplicatedStorage.Events.RightPunchAnimationEvent:FireServer()
							end

							task.wait(0.25)

							canPunch = true
						end
					end
				end)
			end
		end)
		player.Backpack.punch.Unequipped:Connect(function()
			if true then
				timesClicked = 0
				
				game.ReplicatedStorage.Events.punchUnequipEvent:FireServer()
				connection:Disconnect()
				
				for _, displayCharacterObject in pairs(workspaceObject:GetDescendants()) do
					if displayCharacterObject:IsA("Motor6D") then
						if displayCharacterObject.Name == "Left Shoulder" or displayCharacterObject.Name == "Right Shoulder" then
							game.ReplicatedStorage.Events.PassiveEvent:FireServer(displayCharacterObject)
						end
					end
				end
			end
		end)
		
		while task.wait() do
			if connection2 then connection2:Disconnect() end
			if connection3 then connection3:Disconnect() end
			
			for _, workspaceObject2 in pairs(workspace:GetDescendants()) do
				if workspaceObject2.Name == "Left Arm" and workspaceObject2.Parent:FindFirstChildWhichIsA("BallSocketConstraint") and workspaceObject2.Parent ~= workspaceObject then
					if workspaceObject2:GetAttribute("IsPunching") == true then
						connection2 = workspaceObject2.Touched:Connect(function()
							if true and canBePunched == true then
								canBePunched = false

								game.ReplicatedStorage.Events.PunchDamageEvent:FireServer()

								playerHumanoid:SetStateEnabled(Enum.HumanoidStateType.GettingUp, false)
								playerHumanoid:ChangeState(Enum.HumanoidStateType.Ragdoll)
								playerHumanoid:UnequipTools()

								game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)

								task.wait(2)

								playerHumanoid:SetStateEnabled(Enum.HumanoidStateType.GettingUp, true)
								playerHumanoid:ChangeState(Enum.HumanoidStateType.GettingUp)

								game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)

								canBePunched = true
							end
						end)
					end
				end
			end
			for _, workspaceObject2 in pairs(workspace:GetDescendants()) do
				if workspaceObject2.Name == "Right Arm" and workspaceObject2.Parent:FindFirstChildWhichIsA("BallSocketConstraint") and workspaceObject2.Parent ~= workspaceObject then
					if workspaceObject2:GetAttribute("IsPunching") == true then
						connection3 = workspaceObject2.Touched:Connect(function()
							if true and canBePunched == true then
								canBePunched = false

								game.ReplicatedStorage.Events.PunchDamageEvent:FireServer()

								playerHumanoid:SetStateEnabled(Enum.HumanoidStateType.GettingUp, false)
								playerHumanoid:ChangeState(Enum.HumanoidStateType.Ragdoll)
								playerHumanoid:UnequipTools()

								game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)

								task.wait(2)

								playerHumanoid:SetStateEnabled(Enum.HumanoidStateType.GettingUp, true)
								playerHumanoid:ChangeState(Enum.HumanoidStateType.GettingUp)

								game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)

								canBePunched = true
							end
						end)
					end
				end
			end
		end
	end
end

this is the part that the script appears to be skipping:

...and workspaceObject2.Parent:FindFirstChildWhichIsA("BallSocketConstraint") and workspaceObject2.Parent ~= workspaceObject then

i dont know what to do. ive tried using “…and workspaceObject2.Parent ~= character” and “…and workspaceObject2.Parent.Name ~= character.Name…“Clone”” but to no avail. please help

1 Like

nvm, finally tested it without the other account and yeah, it only happens when theres more than one person in the server

1 Like

nvm, whenever someone punched i would set everyones “IsPunching” attribute to true, instead of just the punching player’s. fixed it

1 Like

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