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)
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)
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”.
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.
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)