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


script.Parent.Part.Touched:connect(function(touchedpart) -- okay the tool's part (the key) touched something -- does the touched part have a state (a string value or similar?) local state = touchedpart:FindFirstChild("State") if( state ~= nil )then if( state.Value == "empty" )then -- change state and stuff -- state.Value = "full" elseif( state.Value == "full" )then state.Value = "empty" end end end)

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

local debounce = false

function getPlayer(humanoid) 
local players = game.Players:children() 
for i = 1, #players do 
if players[i].Character.Humanoid == humanoid then return players[i] end 
end 
return nil 
end 

function onTouch(part) 

local human = part.Parent:findFirstChild("Humanoid") 
if (human ~= nil) and debounce == false then

debounce = true

local player = getPlayer(human) 

if (player == nil) then return end 

script.Parent:clone().Parent = player.Backpack

wait(2)
debounce = false
end
end


script.Parent.Parent.Touched:connect(onTouch) 

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)

script.Parent.Part.Touched:connect(function(touchedpart)
    local state = touchedpart:FindFirstChild("State")
    if( state ~= nil )then
        -- door has a state, so it is either open or closed
        if( state.Value == "closed" )then
            -- set the door's state to "open"
            state.Value = "open"
            -- actually move the door/destroy the door so it opens aka perform the action
            -- for example: touchedpart:Destroy()
            -- destroy the key
            script.Parent:Destroy()
        end
    end
end)

If you wanted to give the player an item instead;

local potion = game.Workspace.potion

script.Parent.Part.Touched:connect(function(touchedpart)
    local state = touchedpart:FindFirstChild("State")
    if( state ~= nil )then
        -- door has a state, so it is either full or empty
        if( state.Value == "full" )then
            -- set the door's state to "empty"
            state.Value = "empty"
            local temp = potion:Clone()
            -- put the potion in the player's backpack
            temp.Parent = script.Parent.Parent
        end
    end
end)
Ad

Answer this question