Push player in a direction where the local player is looking

I’m attempting to create an interaction where when one player pushes, it causes the other player to move in the direction the local player is facing.

tool.Handle.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and hit.Parent ~= tool.Parent then
		if Debounce == true and playershit[hit.Parent] == nil then
			local Player = game.Players:GetPlayerFromCharacter(tool.Parent)
			
			hit.Parent:FindFirstChild("Humanoid"):TakeDamage(16)
			hit.Parent:FindFirstChild("Humanoid").PlatformStand = true
			
			local att = Instance.new("Attachment")
			att.Parent = hit
			
			local linear = Instance.new("VectorForce")
			linear.Parent = hit
			linear.Attachment0 = att
			linear.Force = Player.Character.HumanoidRootPart.CFrame.lookVector * 110
			linear.RelativeTo = Enum.ActuatorRelativeTo.World

			playRandomSound(hit, sounds)
			randomdecal(hit, bruises)

			playershit[hit.Parent] = true
			task.wait(1)
			hit.Parent:FindFirstChild("Humanoid").PlatformStand = false
			playershit[hit.Parent] = nil
		end
	end
end)

You can just set their velocity as the look vector of the camera’s CFrame, that should work.

Would you be able to provide me with an example?

Alright, please note this code is for example purposes only and doesn’t work, and wont achieve the results you are looking for its just written to give you an example

The tool’s hierarchy:

image

The ServerManager’s code

local tool = script.Parent
local lookvectorevent = tool:WaitForChild("Pushed")

lookvectorevent.OnServerEvent:Connect(function(plr,lookvector)
	if lookvector then
		plr.Character.HumanoidRootPart.AssemblyLinearVelocity = lookvector*10000 -- I know this will only work on the client, I am just providing this as an example because I did not code it to push other players
	end
end)

The ClientManager’s code

local camera = workspace.CurrentCamera
local pusher = script.Parent:WaitForChild("Pushed")
local tool = script.Parent
local cam = workspace.CurrentCamera
local uis = game:GetService("UserInputService")

uis.InputBegan:Connect(function(inputdata)
	if inputdata.UserInputType == Enum.UserInputType.MouseButton1 or inputdata.UserInputType == Enum.UserInputType.Touch and tool.Parent:IsA("Model") then -- You could also just use Tool.Activated, I just like to use UIS for some reason
		local lookvector = cam.CFrame.LookVector
		if lookvector then
			pusher:FireServer(lookvector)
		end
	end
end)

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.