Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by
Lualaxy 78
4 years ago
Edited 4 years ago

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)
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 User#28017 0 — 4y
0
Well it depends. It starts in game.ServerStorage.Room 201.Script Lualaxy 78 — 4y
0
Then it gets into a players backpack. So when he leaves it goes back into server storage Lualaxy 78 — 4y
0
So the main thing is the tool being Room 201 and the script is in it. Lualaxy 78 — 4y
View all comments (3 more)
0
oh ok User#28017 0 — 4y
0
does the tool show up in the player's backpack? User#28017 0 — 4y
0
yes Lualaxy 78 — 4y

1 answer

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

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

0
Oh I just realised I has PlayerAdded instead of PleaserRemoving -_- let me see if it will work now Lualaxy 78 — 4y
0
No it still doesn't work. Lualaxy 78 — 4y
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. Lualaxy 78 — 4y
0
wait, is the tool a child of ServerStorage or Room 201, or is the tool "Room 201"? User#28017 0 — 4y
View all comments (6 more)
0
The tool is Room 201 Lualaxy 78 — 4y
0
And the tool itself is in ServerStorage Lualaxy 78 — 4y
0
alright i updated the question User#28017 0 — 4y
0
But I have a lot of keys. I don't only have room 201. How do I add more? Lualaxy 78 — 4y
0
oh, do "elseif allChildren[i].Name == " and yeah User#28017 0 — 4y
0
i saved an example in my code User#28017 0 — 4y
Ad

Answer this question