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 get my key to work with my door after it gets cloned?

So basically I made it so when you click a GUI button it clones a key (which is in ReplicatedStorage) and puts it in the players backpack but it doesn't open the door. It works when the key is in Starterpack but when its cloned nothing happens.

Here is my code for the button to clone the key:

local p  = game.Players.LocalPlayer
local Ready = true

script.Parent.MouseButton1Click:Connect(function()
	if Ready == true then
		local tool = game.ReplicatedStorage.Room1:Clone()
		tool.Parent = p.Backpack
		Ready = false
	end
end)

Here is my code for the door to detect the humanoid inside the key:

function ontouch(hit)
	local Doorkey = hit:findFirstChild("Doorkey1") -- Finding the Humanoid in Card Tool
	if Doorkey then -- if Key Touched Then >>
		script.Parent.Transparency = 0.4
		script.Parent.CanCollide = false
		script.Parent.Sign.BrickColor = BrickColor.Green()
		wait(5)-- time for door
		script.Parent.Transparency = 0
		script.Parent.CanCollide = true
		script.Parent.Sign.BrickColor = BrickColor.White()
	end
end
script.Parent.Touched:Connect(ontouch)

Please help, Thanks!

1 answer

✓ Accepted answer

The reason for that ins't working is: you are cloning the tool on the client and this is not replicated to the server, so when you check if the player has the tool on server script, returns false, because it does not have it for the server, only for yourself. How solve it?

First Step: Create a Remote Event on ReplicatedStorage and rename it to 'CloneTool'

Second Step: Put a Local Script inside your Button and write this code

local Player = game:GetService('Players').LocalPlayer
local Ready = false

script.Parent.MouseButton1Click:Connect(function()
   if not Ready then
      Ready = true 
      game.ReplicatedStorage.CloneTool:FireServer()
      wait(.5)
      Ready = false
    end
end)

Third Step: Put a Script in ServerScriptService and write this code(this script will allow us handle the RemoteEvent)

local ReplicatedStorage = game:GetService('ReplicatedStorage')
local CloneEvent = ReplicatedStorage:WaitForChild("CloneTool")

CloneEvent.OnServerEvent:Connect(function(plr)
   local Tool = ReplicatedStorage.Room1:Clone()
   Tool.Parent = plr.Backpack
end)

hope it works (Apologize for my English)

3 comments

0

Thank you so much that worked perfectly!

0

Also how do you check if you have a certain item in your backpack (so I can replace the Ready variable)

0

You can use this to check if game.Players.LocalPlayer:FindFirstChild("ToolName") then

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