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.
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)
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
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:
-- 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