Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

how do i make a key that will destroy after i open door?

hello again, I have a model of a key in my map, if you press on it, then the key (tool) appears in my backpack; if I touch the door with this key, the door opens. I want to make sure that after the door is opened, the key deletes from my backpack. I tried to do it by myself and find info about this on the internet but did not find it. I would be very grateful if someone knows how to do this. sorry if I'm asking too much

door script:

local door = script.Parent.Door
local keystatus = false
door.Touched:Connect(function(hit)
	
local itskey = hit.Parent.Key
	if itskey then 
		door.CanCollide = false
		door.Transparency = 1
	end
	end)
	

key script:

local tool = game.ReplicatedStorage.Key
	local giver = script.Parent
	local canGive = false

giver.ClickDetector.MouseClick:Connect(function(player)
	if canGive == false then 
		canGive = true 
		local clone = tool:Clone()
		clone.Parent = player.Backpack
		wait()
		canGive = false
		game.Workspace.KeyGiver:Destroy()
				
	end

end)

1 answer

✓ Accepted answer

there is a easy way to do this. this is ur script.

local door = script.Parent.Door
	local keystatus = false
	door.Touched:Connect(function(hit)
	 
	local itskey = hit.Parent.Key
	    if itskey then
	        door.CanCollide = false
	        door.Transparency = 1
	    end
	    end)

now to fix it would need some changes i will show

local door = script.Parent.Door
	local keystatus = false
	door.Touched:Connect(function(hit)
	 
	local itskey = hit.Parent.Key
	    if itskey then
            local keytool = hit.Parent.Key.Parent -- im assuming the key is the child of the actual tool.so basically do this.
keytool:Destroy() -- and as simple as that!
	        door.CanCollide = false
	        door.Transparency = 1
	    end
	    end)

1 comment

0

thank you

This discussion is preserved as it was. New answers, comments, and votes are closed.