Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I do interaction (Key and Door) and can get items?

Asked by 7 years ago

This

01script.Parent.Part.Touched:connect(function(touchedpart)
02    -- okay the tool's part (the key) touched something
03    -- does the touched part have a state (a string value or similar?)
04    local state = touchedpart:FindFirstChild("State")
05    if( state ~= nil )then
06        if( state.Value == "empty" )then
07            -- change state and stuff --
08            state.Value = "full"
09        elseif( state.Value == "full" )then
10            state.Value = "empty"
11        end
12    end
13end)

And combine it with this? So I get an item from an interaction? (key vs Door) Or removes your current Tool?

01local debounce = false
02 
03function getPlayer(humanoid)
04local players = game.Players:children()
05for i = 1, #players do
06if players[i].Character.Humanoid == humanoid then return players[i] end
07end
08return nil
09end
10 
11function onTouch(part)
12 
13local human = part.Parent:findFirstChild("Humanoid")
14if (human ~= nil) and debounce == false then
15 
View all 30 lines...

1 answer

Log in to vote
1
Answered by 7 years ago

Assume that you want to destroy the key if a player uses it to open a door;

script.Parent is the tool (the key)

01script.Parent.Part.Touched:connect(function(touchedpart)
02    local state = touchedpart:FindFirstChild("State")
03    if( state ~= nil )then
04        -- door has a state, so it is either open or closed
05        if( state.Value == "closed" )then
06            -- set the door's state to "open"
07            state.Value = "open"
08            -- actually move the door/destroy the door so it opens aka perform the action
09            -- for example: touchedpart:Destroy()
10            -- destroy the key
11            script.Parent:Destroy()
12        end
13    end
14end)

If you wanted to give the player an item instead;

01local potion = game.Workspace.potion
02 
03script.Parent.Part.Touched:connect(function(touchedpart)
04    local state = touchedpart:FindFirstChild("State")
05    if( state ~= nil )then
06        -- door has a state, so it is either full or empty
07        if( state.Value == "full" )then
08            -- set the door's state to "empty"
09            state.Value = "empty"
10            local temp = potion:Clone()
11            -- put the potion in the player's backpack
12            temp.Parent = script.Parent.Parent
13        end
14    end
15end)
Ad

Answer this question