Hit and Touch Function

Hi guys, I’m creating a script that has to detect when a player touches an area, but I don’t know how to make it, I made a script but I don’t know how to make a hit function without using a Touch function

local RemoteEvent = script.Parent.RemoteEvent

RemoteEvent.OnServerEvent:Connect(function(x, argument, value)
	if argument == "Teleport" then
		local TeleportZone = workspace.HousesSpawn.Plot1:FindFirstChild("TeleportZone")
		local OwnerValue = workspace.Plot1.SpawnPart.Owner
	
			local player = game:GetService("Players"):GetPlayerFromCharacter(TeleportZone.Touched.hit.Parent)

			if player then
				print("Touching")
			end
		
	end
end)
2 Likes

if i did’nt want to use touch event, i could use magnitude.

How can I use magnitude to know about the player that touches the part ?

You could use the GetTouchingParts method of a BasePart. You’d want to run that method every heartbeat with RunService and check if one of the touched parts belong to a character.

Measuring distance between humanoid root part and your part with

(Part.Position - HumanoidRootPart.Position)

then you saying if distance more then this length do something.

local Part = script.Parent

game.Players.PlayerAdded:Connect(function(Player)
	local HumanoidRootPart = Player.CharacterAdded:Wait():WaitForChild("HumanoidRootPart")
	
	while wait(1) do
		if (Part.Position - HumanoidRootPart.Position).Magnitude < 4 then
			print("It works but kind of not touching ")
		end
	end
end)

2 Likes

Yeah, to add on this, you can look at “OverlapParams” which is region3 but better, it gives more advantages and aswell gives you a method to grab whats touches it. It’s called “GetPartsinParts”.

I’ve seen that those types of hitboxes are inefficient. Most people uses “OverlapParams” and “RayCastParams”.

I did make a resource for my hitbox which contains “OverlapParams” it works great.

before using mine you can check other’s techniques. Because mine is not touching, just determining length between you and part then doing something. But their’s is touching, not like mine. And as @KultDeeri said, Overlap Params and RayCastParams more efficent. So use them.

Yeah but I only need it to create a GUI that a player can use to kick out others from the plot, so I need to create something easy also for the Server

I solved, I created a variable and when the player click on the GUI, it looks for the variable and if it’s true then the script using the Touch function kick out the player. But I have another error now, I need that only the other players be kicked out and not the owner of the plot, so I added a line of code
if player.Name == Owner.Name then , and this gave to me this error:

Workspace.HousesSpawn.Plot1.House1.TeleportZone.Script:10: attempt to index nil with 'Name

Also the script gave to me this error:

Workspace.HousesSpawn.Plot1.House1.TeleportZone.Script:11: attempt to index nil with 'Character

here the new code

local Part = script.Parent
local Variable = script.Parent.Parent.Teleport
local TeleportPad = script.Parent.Parent.Outside
local Owner = workspace.Plot1.SpawnPart.Owner

Part.Touched:Connect(function(hit)
	local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
	
	if Variable.Value == true then
		if player.Name ~= Owner.Value then
			player.Character:FindFirstChild("HumanoidRootPart").CFrame = CFrame.new(TeleportPad.Position)
		end
	end	
end)

I’m confused so did you fixed it or do you still need help?

1 Like

Yes I fixed the problem I had with Touch function, but now I have the issues I explained above and I don’t know how to fix them