Im trying to make a push script when a player touches one of its bodyparts with another player or dummy with humanoid, and the mouse button is clicked it will push 10 studs.
But its not working and i dont have much time to debug too thats why I came straight here, and my code might have some mistakes.
Local Script:
local player = game:GetService("Players").LocalPlayer
repeat wait()
until player.Character
local char = player.Character
local human = char:FindFirstChild("Humanoid")
local replic = game:GetService("ReplicatedStorage")
local remote = replic:WaitForChild("RemoteEvent")
local UIS = game:GetService("UserInputService")
UIS.InputBegan:Connect(function(input)
if input == Enum.UserInputType.MouseButton1 then
for _,body_part in pairs(char:GetChildren()) do
if body_part:IsA("BasePart") then
body_part.Touched:connect(function(hit_part)
if hit_part.Parent:FindFirstChild("Humanoid") then
local hrp = hit_part.Parent:FindFirstChild("HumanoidRootPart")
if hrp then
local direction = (hrp.Position-body_part.Position).Unit
remote:FireServer(player,direction,hrp)
end
end
end)
end
end
end
end)
Server Script:
local replic = game:GetService("ReplicatedStorage")
local remote = replic:FindFirstChild("RemoteEvent")
remote.OnServerEvent:Connect(function(player,direction,hrp)
hrp.Velocity = direction*10
end)
It would be helpful if someone can fix it or explain
UIS.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
for _,body_part in pairs(char:GetChildren()) do
if body_part:IsA("BasePart") then
body_part.Touched:connect(function(hit_part)
if hit_part.Parent:FindFirstChild("Humanoid") then
local hrp = hit_part.Parent:FindFirstChild("HumanoidRootPart")
if hrp then
local direction = (hrp.Position-body_part.Position).Unit
remote:FireServer(direction,hrp)
end
end
end)
end
end
end
end)
You might need to bump up the Velocity as well to see the push and you might not want to keep adding Touched events every time a mouse is clicked.
Well you had input == Enum before, I made it input.UserInputType == Enum as a first change. Also you fired your remote event and used the player as the first parameter which it would add the player object twice as it is already included by default so you wouldn’t have hrp in the server event and direction would also be the value of the player.
For starters, he fixed line 2 with input since Input.UserInputType is the type of input the player is giving.
He also removed player from your Server Fire Event since the player is the first argument by default for the method. You don’t need to put player for when firing the server, but it usually needs to be there for OnServerFired event.
EDIT: I’ll just carry on my merry way, seeing your message clears throat
I had that exact error until I removed the player from when you fire the event. One thing you can do is actually print out your 3 variables on the server side to see what their values are. Make sure they have the right values. If you removed the player that should have helped though.
Maybe post your code exactly as you are running it now so we can see what you are running with.
local replic = game:GetService("ReplicatedStorage")
local remote = replic:FindFirstChild("RemoteEvent")
remote.OnServerEvent:Connect(function(player,direction,hrp)
local force = Instance.new("BodyForce")
force.Force = direction * 100 -- adjust the magnitude as needed
force.Parent = hrp
wait(0.1) -- wait a short amount of time for the force to take effect
force:Destroy()
end)
Update: No error is showing now
tried a print statement to check if remote event is firing and it is firing so the problem is with pushing the other character
local player = game:GetService("Players").LocalPlayer
repeat wait()
until player.Character
local char = player.Character
local human = char:FindFirstChild("Humanoid")
local replic = game:GetService("ReplicatedStorage")
local remote = replic:WaitForChild("RemoteEvent")
local UIS = game:GetService("UserInputService")
UIS.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
for _,body_part in pairs(char:GetChildren()) do
if body_part:IsA("BasePart") then
body_part.Touched:connect(function(hit_part)
if hit_part.Parent:FindFirstChild("Humanoid") then
local hrp = hit_part.Parent:FindFirstChild("HumanoidRootPart")
if hrp then
local direction = (hrp.Position-body_part.Position).Unit
remote:FireServer(direction,hrp)
end
end
end)
end
end
end
end)
server script:
local replic = game:GetService("ReplicatedStorage")
local remote = replic:FindFirstChild("RemoteEvent")
remote.OnServerEvent:Connect(function(player,direction,hrp)
hrp.Velocity = direction*100
end)