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

local E = script.Parent

local function onTouch(otherPart)
--insert code
end
E.Touched:Connect(onTouch)

Just copy off of a free model or search up a tutorial. This is quite a easy script.

if it’s easy why cant you help…?

Did any of these solutions work for you?

I literally just said that above your post.

I haven’t tried yet, but idk if they understand what I’m trying to say.

Did you try any of them? If so i would like to know if it worked and what you would like to add or if there was an error.

There’s multiple tutorials on similar issues. You should always search up on DevForum or YouTube on how to do these before you create a topic if you have no idea after that.

Try this

SERVER SCRIPT:

local Players = game:GetService("Players")

-- Insert bool value into player upon joining
Players.PlayerAdded:Connect(function(player)
    local hasTouchedGreenPart = Instance.new("BoolValue", player)
    hasTouchedGreenPart.Name = "hasTouchedGreenPart"
end)

-- Part variables
local greenPart = game.Workspace:WaitForChild("green part name here")
local redPart = game.Workspace:WaitForChild("red part name here")
local normalPart = game.Workspace:WaitForChild("normal part name here")

-- Set value to true when player touches green
greenPart.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local player = Players:GetPlayerFromCharacter(hit.Parent)
        player.hasTouchedGreenPart.Value = true
    end
end)

-- When players touches red, teleport them to normal part if they have touched green
redPart.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local player = Players:GetPlayerFromCharacter(hit.Parent)
        if player.hasTouchedGreenPart.Value == true then
            hit.Parent.HumanoidRootPart.Position = normalPart.Position
        end
    end
end)

Sorry if the formatting is wrong this is on a phone :slight_smile:

Edit: I tested it and it works. I hope this is what you were asking for!

1 Like

You can use a ModuleScript to run a script if you’re being specific here.

You need to know how to use functions and events, you’ll have to use the Touched event. Here’s an example:

-- Create a function, I'm going to name it "potato"
-- All this function will do is print "hi"
function potato()
    print( "hi")
end

-- Using the Touched event, make this function run whenever 
-- the part is touched.
script.Parent.Touched:Connect(potato)

-- When anything touches the part this script will print "hi"

Edit: Just realized this post was already solved

1 Like

how could I make this work for multiple ?

  1. Insert more bool values into the player when they join
  2. Declare more part variables
  3. Add more touched events

All three of these can be done by copying parts of my previous script and tweaking them a bit

Example:

--[[
Information:
Green unlocks red
Blue unlocks purple
]]--

local Players = game:GetService("Players")

-- Insert bool value into player upon joining
Players.PlayerAdded:Connect(function(player)
    local hasTouchedGreenPart = Instance.new("BoolValue", player)
    hasTouchedGreenPart.Name = "hasTouchedGreenPart"

    local hasTouchedBluePart = Instance.new("BoolValue", player)
    hasTouchedGreenBlue.Name = "hasTouchedBluePart"
end)

-- Part variables
local greenPart = game.Workspace:WaitForChild("green part name here")
local redPart = game.Workspace:WaitForChild("red part name here")
local bluePart = game.Workspace:WaitForChild("blue part name here")
local purplePart = game.Workspace:WaitForChild("purple part name here")
local normalPart = game.Workspace:WaitForChild("normal part name here")

-- Set value to true when player touches green
greenPart.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local player = Players:GetPlayerFromCharacter(hit.Parent)
        player.hasTouchedGreenPart.Value = true
    end
end)

-- Set value to true when player touches blue
bluePart.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local player = Players:GetPlayerFromCharacter(hit.Parent)
        player.hasTouchedBluePart.Value = true
    end
end)

-- When players touches red, teleport them to normal part if they have touched green
redPart.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local player = Players:GetPlayerFromCharacter(hit.Parent)
        if player.hasTouchedGreenPart.Value == true then
            hit.Parent.HumanoidRootPart.Position = normalPart.Position
        end
    end
end)

-- When players touches purple, teleport them to normal part if they have touched blue
purplePart.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local player = Players:GetPlayerFromCharacter(hit.Parent)
        if player.hasTouchedBluePart.Value == true then
            hit.Parent.HumanoidRootPart.Position = normalPart.Position
        end
    end
end)

As you can see, I have just added a few more variables and changed the functions a little
This should do the trick :+1:

7 Likes