Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Give velocity to a HumanRootPart when touching?

I am making a game where if you are touching another player and clicking a key, then it plays an animation and gives the other player velocity. The problem is, I can't find a way to say "Am I touching another player? If so, give that specific player velocity." If anyone could help, that would be greatly appreciated.

1 answer

✓ Accepted answer
--Services
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--Variables for Player and Mouse
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()

--Event that checks for input
UserInputService.InputBegan:Connect(function(input, proc)
	if not proc then
		if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == Enum.KeyCode.E then
			--Check if target is valid
			if Mouse.Target and Mouse.Target.Parent then
				local Targetted = Players:GetPlayerFromCharacter(Mouse.Target.Parent)
				if Targetted then
					--Fire event to server, go crazy
				end
			end
		end
	end	
end)

I'm assuming you had an issue figuring out how to get a player, so I drew something up.

UIS handles input detection, and the InputBegan event gets whatever input that was initially pressed. From there you just simply need to get the mouse of the player, check if the target exists, and go from there. If you have a question about any of this let me know!

4 comments

0

https://developer.roblox.com/en-us/api-reference/class/UserInputService || https://developer.roblox.com/en-us/api-reference/class/Mouse || if you want to know more about these two, here are the links! Note that UIS is the preferred option almost always as it has better functionality.

0

I read all of the articles and looked through the code. Very helpful! Is this just saying: If the keyboard input is E, then if the mouse is pointing at a player, then it fires an event to the server that fires it to the player that now gets velocity?

0

Exactly.

1

Thanks!

This discussion is preserved as it was. New answers, comments, and votes are closed.