So I’m making a horror game and there’s keys that players can get from the workspace that can unlock doors. But I was thinking that if people are playing with each other and someone gets disconnected with a tool in their inventory that it would then break the game. Does anyone know how to move a tool from a players inventory back to a specific location when they leave? If that’s too difficult then maybe somehow the tool could clone itself when picked up so the players can all get one. I just need someway around this issue. Thanks!
1 Like
In a script in serverscriptservice:
local Players = game:GetService("Players")
local function OnPlayerAdded(Player)
local function OnCharacterRemoving(Character)
for i, v in pairs(Character:GetChildren()) do
if v:IsA("Tool") then
v.Parent = workspace
end
end
for i, v in pairs(Player.Backpack:GetChildren()) do
v.Parent = workspace
end
end
Player.CharacterRemoving:Connect(OnCharacterRemoving)
end
Players.PlayerAdded:Connect(OnPlayerAdded)
I got stuck on the character part because it kept saying nil, so this was Forummer’s structure of code from here
1 Like
First, you have to detect when a player leaves. Use Players.PlayerRemoving event. Then, check if the player has the tool you want to return, and if so, you just clone the tool (I highly recommend to put the tool inside ServerStorage) and set its position to your choice. Something like this:
game:GetService("Players").PlayerRemoving:Connect(function(player)
if player.Backpack:FindFirstChild("ToolName") then
game:GetService("ServerStorage")["ToolName"]:Clone().Parent = workspace
game:GetService("ServerStorage")["ToolName"]:Clone().Position = Vector3.new(x, y, z)
end
)
Tell me if that doesn’t work, so I can help you to solve the issue.
2 Likes