My push script working not as expected

Ok i apologize that i have to post this again…
so my problem is that i’m making a push script
local script:

local player = game:GetService("Players").LocalPlayer
repeat wait()
until player.Character
local char = player.Character
local RelicatedStorage = game:GetService("ReplicatedStorage")
local remote = RelicatedStorage.remote
local UIS = game:GetService("UserInputService")
local inputted = false
local shouldbreak = false
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
				local connection
				connection = body_part.Touched:Connect(function(hit_part)
					if hit_part.Parent:FindFirstChild("Humanoid") then
						local hrp = hit_part.Parent.HumanoidRootPart
						if hrp then
							local direction = (hrp.Position-body_part.Position).Unit
							remote:FireServer(direction,hrp)
							shouldbreak = true
						end
					end
					connection:Disconnect()
				end)
			end
			if shouldbreak then
				break
			end
		end
	end
end)
UIS.InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		shouldbreak = false
	end
end)

server script:

local replic = game:GetService("ReplicatedStorage")
local remote = replic.remote
remote.OnServerEvent:Connect(function(player,direction,hrp)
	print("Server fired")
	local force = Instance.new("BodyVelocity")
	force.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
	force.Velocity = direction*100
	force.P = 10000
	wait(0.1)
	force:Destroy()
end)

i got two issues:

  1. the server is being fired a couple of times when the touched event is fired multiple times but i tried with disconnect() function but still it is firing a couple of times when i touch the other character without clicking
    2.the bodymover is not working thus, not pushing
1 Like

from what im seeing you arent parenting the bodyvelocity to anything and i think you needto parent them (to enemy humanoidrootpart in your case) , also you can just add a cooldown onto the touched thing, for example here

local has_hit =false
body_part.Touched:Connect(function(hit_part)
        
	if hit_part.Parent:FindFirstChild("Humanoid") and not has_hit then
		local hrp = hit_part.Parent.HumanoidRootPart
		if hrp then
                        has_hit = true
			local direction = (hrp.Position-body_part.Position).Unit
			remote:FireServer(direction,hrp)
			shouldbreak = true
		end
	end
end)

i forgot that but now my main issue is the issue 1

Ok to be accurate suppose if i click my mouse 2 times without touching part and when i touch the part the server fires 2 times thats my problem

Ohh i see its because the connection is stil lwaitingf for something to touch it before disconnecting, this might be fix

local player = game:GetService("Players").LocalPlayer
repeat wait()
until player.Character
local char = player.Character
local RelicatedStorage = game:GetService("ReplicatedStorage")
local remote = RelicatedStorage.remote
local UIS = game:GetService("UserInputService")
local inputted = false
local shouldbreak = false

local connection 

UIS.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		if connection ~= nil then
			connection:Disconnect()
		end
		for _,body_part in pairs(char:GetChildren()) do
			if body_part:IsA("BasePart") then
				
				connection = body_part.Touched:Connect(function(hit_part)
					if hit_part.Parent:FindFirstChild("Humanoid") then
						local hrp = hit_part.Parent.HumanoidRootPart
						if hrp then
							local direction = (hrp.Position-body_part.Position).Unit
							remote:FireServer(direction,hrp)
							shouldbreak = true
						end
					end
					connection:Disconnect()
                                        connection = nil
				end)
			end
			if shouldbreak then
				break
			end
		end
	end
end)
UIS.InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		shouldbreak = false
	end
end)

Can be easily replaced by

body_part.Touched:Once(function(hit_part)
					if hit_part.Parent:FindFirstChild("Humanoid") then
						local hrp = hit_part.Parent.HumanoidRootPart
						if hrp then
							local direction = (hrp.Position-body_part.Position).Unit
							remote:FireServer(direction,hrp)
							shouldbreak = true
						end
					end)

sorry for tabs, it just pasted like that

You will avoid using a separate variable for connection.
Second: under any circumstances, do not do checks and calculations on local side. Just FireServer (without any arguments) and do all the calculation server-side.

About your issue, I am currently investigating it.

edit: The issue should be fixed when you will use :Once instead of :Connect.

So what does once do I’m new to scripting and according to my common sense does it run once?

Hey im back i tried the code and still the server is being fired many times with one click.

local player = game:GetService("Players").LocalPlayer
repeat wait()
until player.Character
local char = player.Character
local RelicatedStorage = game:GetService("ReplicatedStorage")
local remote = RelicatedStorage.remote
local UIS = game:GetService("UserInputService")
local inputted = false
local shouldbreak = false
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
				local connection
				connection = body_part.Touched:Once(function(hit_part)
					if hit_part.Parent:FindFirstChild("Humanoid") then
						local hrp = hit_part.Parent.HumanoidRootPart
						if hrp then
							local direction = (hrp.Position-body_part.Position).Unit
							remote:FireServer(direction,hrp)
							shouldbreak = true
						end
					end
					connection:Disconnect()
				end)
			end
			if shouldbreak then
				break
			end
		end
	end
end)
UIS.InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		shouldbreak = false
	end
end)

Tried a different apporach idk if it works

local player = game:GetService("Players").LocalPlayer
repeat wait()
until player.Character
local char = player.Character
local RelicatedStorage = game:GetService("ReplicatedStorage")
local remote = RelicatedStorage.remote
local UIS = game:GetService("UserInputService")
local ContextActionService = game:GetService("ContextActionService")
local shouldbreak =  false
local hit_part
local body_part

local function input(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		if hit_part.Parent:FindFirstChild("Humanoid") then
			local hrp = hit_part.Parent.HumanoidRootPart
			if hrp then
				local direction = (hrp.Position-body_part.Position).Unit
				remote:FireServer(direction,hrp)
				shouldbreak = true
			end
		end
	end
end

for _,v in pairs(char:GetChildren()) do
	body_part = v
	if body_part:IsA("BasePart") then
		body_part.Touched:Connect(function(hit)
			hit_part = hit
			ContextActionService:BindAction("Input",input,false,Enum.UserInputType.MouseButton1)
		end)
		body_part.TouchEnded:Connect(function(hit_part)
			ContextActionService:UnbindAction("Input")
		end)
	else
		continue
	end
	
	if shouldbreak then
		break
	end
end

Hey there

I’ve changed a few things on the code. Removed and changed a few variables. If I’m correct this code should now work fine. Let me know if it doesn’t.

Code:

local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remote = ReplicatedStorage.remote
local UserInputService = game:GetService("UserInputService")

local inputted = false

UserInputService.InputBegan:Connect(function(input, gameProcessed)
    if not gameProcessed and input.UserInputType == Enum.UserInputType.MouseButton1 then
        local Character = LocalPlayer.Character
        if Character then
            for _, body_part in pairs(Character:GetDescendants()) do
                if body_part:IsA("BasePart") and not inputted then
                    inputted = true
                    local connection
                    connection = body_part.Touched:Connect(function(hit_part)
                        local Humanoid = hit_part.Parent:FindFirstChildOfClass("Humanoid")
                        if Humanoid then
                            local HumanoidRootPart = hit_part.Parent:FindFirstChild("HumanoidRootPart")
                            if HumanoidRootPart then
                                local Direction = (HumanoidRootPart.Position - body_part.Position).Unit
                                Remote:FireServer(Direction, HumanoidRootPart)
                                connection:Disconnect()
                            end
                        end
                    end)
                    break
                end
            end
        end
    end
end)

UserInputService.InputEnded:Connect(function(input, gameProcessed)
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        inputted = false
    end
end)

Oh bruh it doesnt even fire server nvm

lemme test it but i just wanna ask you removed the shouldbreak boolean i put so it means it will break even if it doesnt find a child that is not a basepart?

I removed the shouldbreak and replaced it with the inputted variable so it keeps track of whenever the mouse button is being held down.

anyway the real problem is the server being fired multiple times also when i click the mouse without touching then i touch without clicking the server fires that times so i need something which can check if character is touching otherwise it will break or return nil

So it means your code didn’t work :frowning:

Can you be a bit more clear and also state what exactly you want to be changed?

idk if it work wiith debounce but the issue is
Suppose i click my mouse without touching the part two times then when i touch the part(the chracter to be pushed) the code executes two times i.e, the server fires two times…
Sorry for the trouble anyway

No worries and yes, you should implement a debounce function so it doesnt get loop fired.

Im not that experienced in scripting but i will try

Idk if it is right but here’s the updated code

local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remote = ReplicatedStorage.remote
local UserInputService = game:GetService("UserInputService")
local debounce = false-----------------------------
local inputted = false

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if not gameProcessed and input.UserInputType == Enum.UserInputType.MouseButton1 then
		local Character = LocalPlayer.Character
		if Character and not debounce then
			debounce = true--------------------------
			for _, body_part in pairs(Character:GetDescendants()) do
				if body_part:IsA("BasePart") and not inputted then
					inputted = true
					local connection
					connection = body_part.Touched:Connect(function(hit_part)
						local Humanoid = hit_part.Parent:FindFirstChildOfClass("Humanoid")
						if Humanoid then
							local HumanoidRootPart = hit_part.Parent:FindFirstChild("HumanoidRootPart")
							if HumanoidRootPart then
								local Direction = (HumanoidRootPart.Position - body_part.Position).Unit
								Remote:FireServer(Direction, HumanoidRootPart)
								connection:Disconnect()
								wait(1)
								debounce = false --------------
							end
						end
					end)
					break
				end
			end
		end
	end
end)

UserInputService.InputEnded:Connect(function(input, gameProcessed)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		if debounce then
			debounce = false---------------------------
		end
	end
end)