This
01 | script.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 |
13 | end ) |
And combine it with this? So I get an item from an interaction? (key vs Door) Or removes your current Tool?
01 | local debounce = false |
02 |
03 | function getPlayer(humanoid) |
04 | local players = game.Players:children() |
05 | for i = 1 , #players do |
06 | if players [ i ] .Character.Humanoid = = humanoid then return players [ i ] end |
07 | end |
08 | return nil |
09 | end |
10 |
11 | function onTouch(part) |
12 |
13 | local human = part.Parent:findFirstChild( "Humanoid" ) |
14 | if (human ~ = nil ) and debounce = = false then |
15 |
Assume that you want to destroy the key if a player uses it to open a door;
script.Parent is the tool (the key)
01 | script.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 |
14 | end ) |
If you wanted to give the player an item instead;
01 | local potion = game.Workspace.potion |
02 |
03 | script.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 |
15 | end ) |