My code is not working as expected

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

2 Likes

Try this:

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.

1 Like

so what did u change here?
character limittttttttttttttttt

not working toooooooooooooooooooooooooooooo sorry character limit

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


throwing up error

hrp is userdata for some reason
image

in server script i have player as the first parameter on onserverevent should i remove it

No, because it is an argument that is sent through automatically by roblox for OnServerFired events.

Could you try using LinearVelocity and not the Velocity from the basepart?

Also this is not to fix your problem, just trying to give you a tip.

anyway how do i fix my error
???

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.


like i said hrp returning userdata type and chatgpt also told me to check the class of hrp

image

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)

Updated the code and still not working

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

Here’s is the code again
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.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)

is there anyway i could fix it?