Area based script

Does anyone know a way to have actions happen when a player is within a specific part?

For example, A player walks into this part and is slowed down to a WalkSpeed of 4.
If they leave the area of the part, or the clone of the part is deleted with :Destroy()

I have tried a couple ways of doing this but the .Touched and .TouchEnded events are much too unreliable and don’t do what I would want them to do. I tried another more advanced way but wouldn’t revert the effects when a clone of the part is destroyed. Any tips?

4 Likes

use :GetTouchingParts() to see if a humanoidrootpart is touching it. If so, do the next couple actions. Make sure you have GetTouchingParts() in a loop so it’ll remove as they leave.

Or I have a public model for this:

1 Like

I Checked out your model but can’t quite figure out how I would get it to work for what I am trying to do.

(Create a part through :Clone, any player who steps in that part gets slowed, if the part is destroyed or player steps out, they return to normal)

2 Likes

what if the script was in the players legs?
i imagine something like:

local leg = script.Parent

leg.Touched:Connect(function(hit)
     for _,v in leg:GetTouchingParts() do
         if v.Name == "insert name of the slowing part here" then ------ you could also use :FindFirstChild() for some sort of value that shows that the part slows people        
             leg.Parent.Humanoid.WalkSpeed = ----- whatever speed here
        end
     end
end)


leg.TouchEnded:Connect(function(UnTouchedPart)
    if UnTouchedPart.Name == "part's name" then
        leg.Parent.Humanoid.WalkSpeed = 16
        UnTouchedPart:Destroy()
    end
end)

because the leg will get touched a lot this should refresh enough to set the players speed back to normal. as I said above you can swap it’s name for a check with :FindFirstChild() to see if the part has some value that classifies it as a slowing part.

edit: forgot to destroy the part

1 Like

It is more reliable if you change the contact point to the legs but still has occasional bugs (Along with not working with :destroy)

I use this method and it seems to work consistently.

local pad = YourPart

pad.Touched:Connect(function(otherPart)
	if add == true then
		add = false
		if players:FindFirstChild(otherPart.Parent.Name) then
			if not table.find(TouchingTable, otherPart) then
				table.insert(TouchingTable, otherPart)

				print(otherPart)

			end	
		end
		add = true
	end
end)
pad.TouchEnded:Connect(function(otherPart)
	if players:FindFirstChild(otherPart.Parent.Name) then
		if table.find(TouchingTable, otherPart) then
			table.remove(TouchingTable, table.find(TouchingTable, otherPart))
			if #TouchingTable == 0 then

				print("No touching parts.")

				-- toggle CanTouch so it will trigger any touch actions for players if one is currently touching the part
				
				pad.CanTouch = false; task.wait(1); pad.CanTouch = true

			end
		end
	end	
end)

Shouldn’t this work with Region3? Region3 | Documentation - Roblox Creator Hub
https://youtu.be/L_Jga2MgRdU?si=qQhVuHfaTDKLkvw9
I’ve not used them myself yet, so can’t write a script example for you.

it should have a script with directions in it.

Worked great for me. Only thing is I don’t quite understand where the table comes into play here. Could you please explain it to me?

I grabbed a script that was not correct. Sorry, it won’t work for multiple people as it was posted.

Use this one and make it a Local Script:

local players = game:GetService("Players")
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

local ws = game:GetService("Workspace")
local part = ws:WaitForChild("Part")
local TouchingTable = {}
local touchPadActive = false



part.Touched:Connect(function(otherPart)
	if otherPart.Parent == player.Character then
		if not table.find(TouchingTable, otherPart) then
			table.insert(TouchingTable, otherPart)
			touchPadActive = true

			print(player.Name, "is touching part.")

		end
	end
end)
part.TouchEnded:Connect(function(otherPart)
	if otherPart.Parent == player.Character then
		if table.find(TouchingTable, otherPart) then
			table.remove(TouchingTable, table.find(TouchingTable, otherPart))
			if #TouchingTable == 0 then
				touchPadActive = false

				print(player.Name, "no longer touching part.")

			end
		end
	end
end)

You can convert it to a Server Script if needed. It’s just not setup that way at the moment.

The table is because Roblox touch events are unreliable.

It keeps track of all touching parts and will only stop when ALL parts stop touching.

It makes the touch even more reliable.

I had just filled in the stuff you left out such as the variables and tables so it would work but thank you for the updated script.

Region3 is now deprecated. It is replaced by spatial query API.

1 Like

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