I am trying to make a key door that unlocks if you have the key parented to the players character.
I made a testing block and whenever I click it and get the players characters children, the key doesn’t show up at all!
This is my script:
local key = "TestKey1"
local clickDetector = script.Parent.ClickDetector
local runService = game:GetService("RunService")
clickDetector.MouseClick:Connect(function(player)
local hasKey = false
for i, child in ipairs(player.Character:GetChildren()) do
print(i, child)
end
if hasKey then
print("has key")
else
print("no key")
end
end)
(Just a note, I know I didn’t do anything with the “hasKey” variable but I’m just looking for why the :GetChildren() isn’t working.)
This is what is parented to the character when I clicked the part:
19:38:42.710 1 HumanoidRootPart - Server - Script:8
19:38:42.712 2 Head - Server - Script:8
19:38:42.714 3 LeftHand - Server - Script:8
19:38:42.715 4 RightHand - Server - Script:8
19:38:42.717 5 LeftLowerArm - Server - Script:8
19:38:42.718 6 RightLowerArm - Server - Script:8
19:38:42.720 7 LeftUpperArm - Server - Script:8
19:38:42.722 8 RightUpperArm - Server - Script:8
19:38:42.723 9 LeftFoot - Server - Script:8
19:38:42.725 10 LeftLowerLeg - Server - Script:8
19:38:42.726 11 UpperTorso - Server - Script:8
19:38:42.728 12 LeftUpperLeg - Server - Script:8
19:38:42.729 13 RightFoot - Server - Script:8
19:38:42.731 14 RightLowerLeg - Server - Script:8
19:38:42.732 15 LowerTorso - Server - Script:8
19:38:42.733 16 RightUpperLeg - Server - Script:8
19:38:42.735 17 Humanoid - Server - Script:8
19:38:42.736 18 FirstPersonBody - Server - Script:8
19:38:42.738 19 Animate - Server - Script:8
19:38:42.740 20 Health - Server - Script:8
19:38:42.741 21 Body Colors - Server - Script:8
19:38:42.741 22 Shirt - Server - Script:8
19:38:42.742 23 Pants - Server - Script:8
19:38:42.743 24 blushbaseAccessory - Server - Script:8
19:38:42.743 25 BCHardHatGolden - Server - Script:8
19:38:42.744 26 Broken Dark Demon Horns - Server - Script:8
19:38:42.745 27 Floating Crown - Server - Script:8
19:38:42.746 28 Horror White Face - Server - Script:8
19:38:42.748 29 Tie - Server - Script:8
19:38:42.749 no key - Server - Script:13
I have looked for solutions on the developer hub but there was nothing that was similar to this problem. One person even said they were able to fix it by restarting their studio but I tried that days ago and it didn’t help.
Somebody please help me with this! I don’t understand why studio is doing this.
I’m assuming that the TestKey1 block in your character model is not welded to your character. So when the game loads, it quite literally falls out of the map and deleted. Therefore, your ClickDetector script wasn’t able to find the key at all.
Try welding that key block to the HumanoidRootPart or create a value named “TestKey1” instead.
I forgot to mention it but I have already welded the part to the characters hand. I also have changed it to not anchored and made it’s CFrame position to the CFrame of the right hand in another inventory script.
I have also tried the value before but it didn’t help.
at what point does the hasKey value become true?
edit:
local key = "TestKey1"
local clickDetector = script.Parent.ClickDetector
local runService = game:GetService("RunService")
clickDetector.MouseClick:Connect(function(player)
local hasKey = false
for i, child in ipairs(player.Character:GetChildren()) do
print(i, child)
if child.Name == "Key" then
hasKey = true
end
end
if hasKey then
print("has key")
else
print("no key")
end
end)
Are you creating and welding the “TestKey1” instance on the client or sever? It seems as though the code you provided is being run in a server script, meaning that if you created the key instance on the client the server wouldn’t see it. Try verifying that the server recognizes the key instance.
Note: You can use ‘FindFirstChild’ to check whether an instance is a child of another instance:
player.Character:FindFirstChild("TestKey1") -- key name
No, it is an existing object within the game and when the player clicks on it, they “pick it up” which parents it to the players backpack. When the user clicks a button that has the same name as the key, the key is parented to the player using a local script. And I have also tried to use the FindFirstChild before and it still didn’t work.
Sounds like either, it is only reparenting the key on the client, or it is a timing issue.
Try looping the contents of the backpack to see if it’s still there. If it is I would try reparenting the key to the player character on the server first.
So, basically, you must do an if statement if the player holds the key, or I mean has the key inside of the player in Workspace.
local key = "TestKey1"
local clickDetector = script.Parent.ClickDetector
local runService = game:GetService("RunService")
clickDetector.MouseClick:Connect(function(player)
local hasKey = false
for i, child in ipairs(player.Character:GetChildren()) do
print(i, child)
end
if key == player.Character then
hasKey = true
else
hasKey = false
print(player.Name.." doesn't have the key!")
return
end
if hasKey then
print("has key")
else
print("no key")
end
end)
Because you’re parenting the key instance to the character with a localscript, the server will not recognize that change and therefore cannot see the key instance within the character. I recommend informing the server when you would like to equip the key and having the key parented to the character on the serverside.
You could also take advantage of Tool objects and simply call ‘EquipTool’ or ‘UnequipTools’ on the player’s humanoid instance. Equipping a tool, even if done on the client, will replicate to the server and can therefore be indexed within a server script.
It worked!!! I was able to get the inventory system to work the exact same as before but this time it uses events and doesn’t use a local script. The :GetChildren() was able to find the key inside the player! Thank you so much!