Code issues with push PushForce

what am i doing wrong with the PushForce?
I need to get another player pushed

CLIENT SCRIPT

if input.UserInputType == Enum.UserInputType.MouseButton1 then
		-- code
		Animator:LoadAnimation(normalPunch):Play()
		
		if not gameProcessed then
			print("It has worked! great")
			script.Parent.con1:FireServer()
		else
			print("Something is wrong, check the code.")
		end
	end

SERVERSCRIPT:

event1.OnServerEvent:Connect(function(plr, player, victim)
	-- stam decrease
	local AttackDist = 15 --If a player is further than this value, they won't be damaged.

	for i,v in pairs(game.Players:GetChildren()) do --Loops through all players.		
		if v.Character ~= nil and plr.Character ~= nil and v ~= plr then --Checks if there's a character
			local pRoot = plr.Character:FindFirstChild("HumanoidRootPart")
			local vRoot = v.Character:FindFirstChild("HumanoidRootPart")

			if pRoot and vRoot and (plr.Character.HumanoidRootPart.Position - v.Character.HumanoidRootPart.Position).Magnitude < AttackDist then --Checks if the Root parts exist and if the player is in range.
				local Hum = v.Character:FindFirstChild("Humanoid")

				if Hum and Hum.Health > 0 then --Checks if player is alive.
					Hum.Health -= 20 --Change "100" to the damage the player should take.
					local text = { -- array containing random text
						"You hit hard!",
						"Keep it up!",
						"You can do this!",
						"Just a bit more!!"
					}
					local textLabel = script.Parent.HitsVar
					textLabel.Text = text[math.random(1, #text)]
					wait(3)
					textLabel.Text = ""
					
					local PushForce = Instance.new("BodyVelocity")
					PushForce.Name = "PushForce"
					PushForce.MaxForce = Vector3.new(100000000,100000000 ,100000000)
					PushForce.Velocity = (-victim.HumanoidRootPart.CFrame.LookVector)*100
					PushForce.Parent = victim.HumanoidRootPart
					
					wait(0.25)
					PushForce:Destroy()					
				end

				--Stuff that happens when a player is in range.
			end
		end
	end
	-- end
end)

First off, when you fire the server you don’t give any parameters. You need to fire the server with the “Player” and the “Victim” parameters.

and what about the plr? i am using that… :frowning:

When you use a remote event, the server automatically makes the first parameter the plr who fired the server.

--local script
local string1 = "Pizza"
local string2 = "Burger"
remotevent:FireServer(string1, string2)


--server script
remotevent.OnServerEvent:Connect(function(player, word1, word2)
     print(player, word1, word2)
      --output:
      --MatiasHarders, "Pizza", "Burger"
end)

I am a bit confused, how could i fix that? :frowning:

On the server script, you have 3 parameters

Now the plr parameter is already accounted for (it’s the player who fired the server). For the other 2, you need to define those parameters on the Client side if you are going to use them on the Server side.

--local script
local plr = --define
local victim = --define
script.Parent.con1:FireServer(plr, victim)
1 Like

Oh okay! understand… and after that how could i fix the push force? I need to get another player pushed

Yeah, I would take out the “player” parameter in the server script because you don’t use it. Then on the local script, when they press down, check if the mouse is pointing towards a player. If it is, then make a variable for the “victim”, and fire the server with that parameter.

You can do this like this:

local target = mouse.Target
if target then --check if the mouse points at anything
        local player = game.Players:GetPlayerFromCharacter(target.Parent) --check if the mouses target (maybe a character) is the character of a player
        if player then --check if it was pointing to a player
                script.Parent.con1:FireServer(player) --fire the server with the victim
        end
end

like this? could i need to make another fireserver for the mouse?

		local Player = game.Players.LocalPlayer
		local Mouse = Player:GetMouse()
		script.Parent.con1:FireServer(Mouse)

Not exactly, you want to tell the server the victim, not the mouse. I edited my post above.

1 Like

in the server something like this?

event1.OnServerEvent:Connect(function(plr, victim)

local PushForce = Instance.new(“BodyVelocity”)
PushForce.Name = “PushForce”
PushForce.MaxForce = Vector3.new(100000000,100000000 ,100000000)
PushForce.Velocity = (victim.HumanoidRootPart.CFrame.LookVector)*100
PushForce.Parent = victim.HumanoidRootPart

1 Like

Thats great, the only problem is that the code I showed up above fires the server with the player’s character, therefore the “victim” parameter is the player, not the character. I would just change these two lines:

change to:

PushForce.Velocity = (victim.Character.HumanoidRootPart.CFrame.LookVector)*100
PushForce.Parent = victim.Character.HumanoidRootPart
2 Likes

Thanks you a lot! One last thing… how could I make the push him flying away random?

like math.random → Sometimes he’s pushed away and sometimes he’s not

One second, brb in 3 minutes.____

1 Like

Sure! thanks you :smiley: :smiley: :smiley:

Sorry nvm, alright I’m back. You mean like 50% chance of it working, 50% of them not moving?

1 Like

Yes! exactly that :smiley: :smiley:

Well when they click you can just do this:

local randomnumber = math.random(1,2)

if randomnumber == 1 then
   --fire server
end

Oh you mean something like this?

-- mouse
			local randomnumber = math.random(1,2)

			local Player = game.Players.LocalPlayer
			local Mouse = Player:GetMouse()
			local target = Mouse.Target

			if randomnumber == 1 then
				--fire server
				if target then
					local player = game.Players:GetPlayerFromCharacter(target.Parent)
					if player then
						script.Parent.con1:FireServer(player)
					end
				end
				-- end
			end
1 Like

Yes exactly!!! ______________

1 Like