So I’m making a game where when you start a demon spawns and you have to kill it then when you kill it a key drops and you can use that key to open a door then move on. Does anybody have a clue on how I can do that?
Working off what BRO_ITmeYO proposed, his code required a loop in order to check instances for the death of the demon which is not ideal. A better solution would be to use the function Died for the humanoid class and then destroy the demon so that there is no position conflicts. Also a clone of the original key would be useful in order to keep it for later uses such as other players joining the game.
01 | -- Variables -- |
02 | local demon = game:GetService( "Workspace" ):WaitForChild( "Demon" ) |
03 | local key = game:GetService( "ServerStorage" ):FindFirstChild( "Key" ) |
04 |
05 | if Demon:FindFirstChild( "Humanoid" ) then |
06 | local humanoid = Demon.Humanoid |
07 | humanoid.Died:connect( function () |
08 | local keySpawn = key:Clone() |
09 | keySpawn.Parent = game.Workspace |
10 | keySpawn.Position = demon.HumanoidRootPart.Position |
11 | demon:Destroy() |
12 | end ) |
13 | end |
I hope I helped and let me know if this works.
~RGRage
1 | local demon = game.Workspace.Demon |
2 | local key = game.ServerStorage.key |
3 |
4 | while wait( 1 ) |
5 | if demon.humanoid.health = 0 then |
6 | key.Parent = game.Workspace |
7 | key.Position = demon.HumanoidRootPart.Position + Vector 3. new( 0 , 1 , 0 ) |
previous post explained the key part, to make it open the door:
1 | door.touched:Connect( function (key)) |
2 | if key.Parent.Name = = "Handle" and key.Parent.Parent.Name = = "Key" then |
3 | door.cancollide = false |
4 | wait( 3 ) |
5 | door.cancollide = true |
6 | end ) |