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

Why does my key return script not return the key but also not return any errors?

So I wanted to make a key return script for a resort so the checkin system wouldn't copy keys but move them so no duplicates go to players. Anyways thats not important but whats important is that my script doesn't return the key but it also doesn't return any errors so I don't know what to do. I have been trying to fix it for 2 days now but I have no clue what to do.

So a little idea on how it works: The script is placed inside of the tool (for example lets say the tool: Room 201) The thing is if the tool's parent isn't ServerStorage is checks if the player that left had any keys. So the scripts in the keys execute. If the player that left had a key, then that key should return to the server storage.

game.Players.PlayerRemoving:Connect(function(plr)
		local FKey = script.Parent
		if FKey.Parent ~= game.ServerStorage then	
	if plr.Name == script.Parent.Parent.Parent.Name then -- Targets player on game.Players
	local BP = script.Parent.Parent
	if BP then
		local KeyN1 = script.Parent
		if KeyN1 then
		if KeyN1.Parent == BP then KeyN1.Parent = game.ServerStorage -- Assumes the key is in the backpack
			print("Key Returned") 
		elseif KeyN1.Parent  ~= BP then -- if it isn't in the backpack we search in the players character
		local Char = game.Workspace:FindFirstChild(plr.Name)	
			if Char then local Key = Char:FindFirstChild(script.Parent.Name) -- Searches for the specific key
				if Key then Key.Parent = game.ServerStorage
					print("Key returned")
				end
			end
		end
		end
		end
		else return end 
	else return end
end)

7 comments

0

Can you state what this script is a child of and what its hierarchy is? For example, the script being game.Workspace.Model1.CoolPart.Script

0

Well it depends. It starts in game.ServerStorage.Room 201.Script

0

Then it gets into a players backpack. So when he leaves it goes back into server storage

0

So the main thing is the tool being Room 201 and the script is in it.

0

oh ok

0

does the tool show up in the player's backpack?

0

yes

1 answer

The script won't run because when the script is put into a tool that the client has, it wont run on the server, only the client. Here's how to properly put it back into serverstorage:

PUT THIS IN SERVERSCRIPTSERVICE

game.Players.PlayerRemoving:Connect(function(client)
	local tool = client.Backpack["Room 201"] -- or whatever the name of your tool is
	if tool == nil then 
		local char = client.Character
		local allChildren = char:GetChildren()
		for i = 1, #allChildren do 
			if allChildren[i].IsA("Tool") then 
				if allChildren[i].Name == "Room 201" then 
					allChildren[i].Parent = game.ServerStorage
				elseif allChildren[i].Name == "yeyt" then -- or whatever other weapons you have 
					allChildren[i].Parent = game.ServerStorage
				end
			end
		end
	end
	tool.Parent = game.ServerStorage
end)

Props to @Prestory for helping me with this answer

10 comments

0

Oh I just realised I has PlayerAdded instead of PleaserRemoving -_- let me see if it will work now

0

No it still doesn't work.

0

The thing is if the tool's parent isn't ServerStorage is checks if the player that left had any keys. So the scripts in the keys execute. If the player that left had a key, then that key should return to the server storage.

0

wait, is the tool a child of ServerStorage or Room 201, or is the tool "Room 201"?

0

The tool is Room 201

0

And the tool itself is in ServerStorage

0

alright i updated the question

0

But I have a lot of keys. I don't only have room 201. How do I add more?

0

oh, do "elseif allChildren[i].Name == " and yeah

0

i saved an example in my code

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