How could I make a script run if u touch a part?

How could I make a script run if I touch a part?

Here’s what I’m doing:

  • So I made a script that teleports the player to another part if u touch a certain part. there are 2 parts, green and red. and if u touch red part u get teleported to a different part (not green part) the thing is I want it so if u spawn in and touch the red part, nothing happens and u need to touch the green part for that script to work.
9 Likes

Have you tried touched?

1 Like

I tried that but it didnt work, I may be doing something wrong… so I deleted all the scripts for that and posted here.

I don’t think it’s possible to detect when a part is touched without using the touched function…

You should add the touched function back and add a print() on every other line so you can see where you messed up and how to fix it.

Ik I have to use touched but idk what to do after.

My bad, I didn’t understand your issue correctly.

Maybe you could make a script like this,

game.Players.PlayerAdded:Connect(function(plr)
	Instance.new("BoolValue",plr)
	plr.BoolValue.Name = "TeleWork"
	plr.TeleWork.Value = false
	wait(5) -- how long u should wait for
	plr.TeleWork.Value = true
end)

and you can check if TeleWork is true in your teleport script, and if it’s not true, it wont teleport

2 Likes

I don’t want to wait 5 seconds, not any time I want it to work when a part named “Start” is touched sorry if ur confused.

1 Like

Have you tried something like this:

local players = game:GetService("Players")

local startPart = game:GetService("Workspace").StartPart --Change 'StartPart' to whatever your part is called.
local otherPart = game:GetService("Workspace").OtherPart --Change 'OtherPart' to whatever your part is called.

startPart.Touched:Connect(function(hit)
    local plr = players:GetPlayerFromCharacter(hit.Parent)
    if plr then
        local HRP = plr.Character:WaitForChild("HumanoidRootPart")
        HRP.CFrame = otherPart.CFrame
    end
end)

If you don’t want anything to happen if you touch a certain part, then just don’t have a .Touched function for that specific part.

1 Like

Then remove it and make TeleWork true when you want the player be able to teleport.

1 Like

Is this supposed to teleport the player to the other part when touched, if so this is not what I mean. I explained it the best way as possible let me restate this again.

Here’s the parts with the scripts. (These scripts are not needed for this topic) and the part named “Part” is the part they will be teleported to.
image

Now… I have this script in the Stop.
image
Here’s what’s inside.

if hit.Parent:FindFirstChild("Humanoid") then
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	local Teleport = "Part"
	function Touch(hit) --Indicates that the Part has been Touched.
		if script.Parent.Locked == false and script.Parent.Parent:findFirstChild(Teleport).Locked == false then script.Parent.Locked = true script.Parent.Parent:findFirstChild(Teleport).Locked = true --Checks Debounce.
			local Pos = script.Parent.Parent:findFirstChild(Teleport) --Gets the Part to teleport to.
			hit.Parent:moveTo(Pos.Position) wait(1) script.Parent.Locked = false script.Parent.Parent:findFirstChild(Teleport).Locked = false end end --Takes you there and Ends the Function.
	script.Parent.Touched:connect(Touch) --Listens out for Touchers.
end

So this script works, it teleports me to the part in workspace! The thing is…
I want it so if the “Start” part is touched THEN the script above works…

What I mean in short text:

  • If I spawn and touch the Stop part it doesn’t teleport me to the part. but if I touch the Start part and then touch the red part it teleports me.
1 Like

ur script didnt work, so Idk. I made a new message below if that helps better.

Can you please read it right? Here’s what I’m trying to do PART 3. : /

  • So I have this script where if you touch the red part you get teleported to a other part (THIS PART IS NOT THE GREEN AND NOT THE RED PART, IT TELEPORTS THEM TO THE NORMAL PART that you can spawn easily.

  • I’m trying to make the TELEPORT script NOT WORK until I touch the GREEN PART.

  • Once I have touched the GREEN part the TELEPORT script starts working, so IF I touch the red part it teleport me to the NORMAL PART (NOT GREEN PART NOR RED)


What I’m supposed to get when I’ts done.

  • When I spawn and touch the red part I DON’T teleport.

  • BUT when I touch the GREEN part and then I touch the RED part I get teleported.

I’m very sorry for using too much BOLDS, CAPS. Etc… I’m just really stressed right now. I explained in the BEST and understandable WAY possible. but you don’t understand I really hope this makes sense for you.

I tried that, it didn’t work. Also if this doesn’t make sense to you then just stop helping me. I need someone that understands, I don’t have time to right another paragraph to someone who called this the most confusing way.

Heres how to can get the humanoid

tartTeleport.Touched:Connect(function(teleport)

	if teleport.Parent:FindFirstChild("Humanoid") then

	end
	
end)

Heres how to teleport

local StartTeleport = --the name of the block that starts the teleport
local EndTeleport = --the name of the block that you teleport to

StartTeleport.Touched:Connect(function(teleport)

	if teleport.Parent:FindFirstChild("Humanoid")  then
		local char = teleport.Parent
		local HumanoidRootPart = char.HumanoidRootPart
		
		HumanoidRootPart.Position = EndTeleport.Position
	end
	
end)
1 Like

Thats ok, You can modify mine if you want

Oh, sorry I forgot Characters already have physics to prevent being the the part lol

Thats ok, i hope you can modify or use my script

@Oozymage Try interact events GetService(TouchService(Hit)) or same think

Use this in a serverscript (NOT a local script)

local part = script.Parent

local function onTouch(otherPart)
-- code goes here --
end
part.Touched:Connect(onTouch)

This usually works for me, put the code needed in the onTouch function.

To teleport:

local part = script.Parent
local teleportPart = --Instance of part goes here--

local function onTouch(otherPart)
if otherPart.Parent:IsA("Model") then
otherPart.Parent:SetPrimaryPartCFrame(TeleportPart.CFrame)
end
end
part.Touched:Connect(onTouch)

That should work.