Yo, so basically im trying to make a looting system in which when a player dies, a part spawns on him and when someone interacts with the part they get the dead players tools.
local script in startercharacterscript:
local Player = game.Players.LocalPlayer
local Char = Player.Character
local tools = Player.Backpack:GetChildren()
Char:WaitForChild("Humanoid").Died:Connect(function()
game.ReplicatedStorage.RemoteEvent:FireServer(tools)
end)
script in serverscriptservice:
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player, tools)
local Character = player.Character
local part = Instance.new("Part")
part.Name = "Loot"
part.CFrame = Character:FindFirstChild("HumanoidRootPart").CFrame
part.Parent = game.Workspace
local Proximity = Instance.new("ProximityPrompt", part)
Proximity.Triggered:Connect(function(TheOneWhoTriggered)
tools.Parent = TheOneWhoTriggered.Backpack
part:Destroy()
end)
end)
Oh. It looks like you only use GetChildren() before they die, not once they actually die. I would also try to check for tools in the character as well.
I’m not the smartest person, but I believe the issue is that you’re sending over a table of the player’s tools and then trying to assign that directly to the player’s backpack so I would try making a loop that goes through the table and assigns each tool to the player’s backpack one by one
for _, v in tools do
v.Parent = TheOneWhoTriggered.Backpack
end
edit: realized this just after posting, each tool would get deleted after the character died. you would have to clone each tool and parent it to ServerStorage or something
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player)
local backpack = player.Backpack
local items = backpack:GetChildren()
local replicated = game.ReplicatedStorage
local foldernew = Instance.new("Folder")
foldernew.Parent = replicated
items.Parent = foldernew
local part = Instance.new("Part")
part.Name = "Loot"
part.CFrame = player.Character:FindFirstChild("HumanoidRootPart").CFrame
part.Parent = game.Workspace
local Proximity = Instance.new("ProximityPrompt", part)
Proximity.Triggered:Connect(function(TheOneWhoTriggered)
for _, v in foldernew do
v.Parent = TheOneWhoTriggered.Backpack
end
part:Destroy()
end)
end)
and now everytime i interact with the part i get
ServerScriptService.Script:14: attempt to iterate over a Instance value
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player, tools)
local Character = player.Character
local part = Instance.new("Part")
local toolfolder = Instance.new("Folder")
toolfolder.Name = "toolfolder_"..player.Name
toolfolder.Parent = game.ServerStorage
for _, tools in tools do
tools.Parent = toolfolder
end
part.Name = "Loot"
part.CFrame = Character:FindFirstChild("HumanoidRootPart").CFrame
part.Parent = game.Workspace
local Proximity = Instance.new("ProximityPrompt", part)
Proximity.Triggered:Connect(function(TheOneWhoTriggered)
for _, tools in toolfolder:GetChildren() do
tools.Parent = TheOneWhoTriggered.Backpack
end
part:Destroy()
toolfolder:Destroy()
end)
end)
Local Script:
local Player = game.Players.LocalPlayer
local Char = Player.Character
Char:WaitForChild("Humanoid").Died:Connect(function()
local tools = Player.Backpack:GetChildren()
game.ReplicatedStorage.RemoteEvent:FireServer(tools)
end)
i think its not a good idea to send itens from local to server… a exploiter can put how amount of itens that he wants in their backpack and the server will load all of these itens
local function PickupItens(plr, toolfolder, part)
for _, itens in toolfolder:GetChildren() do
itens.Parent = plr.Backpack
end
part:Destroy()
toolfolder:Destroy()
end
game.ReplicatedStorage.RemoteEvent.OnServerEvent.OnServerEvent:Connect(function(player)
local tools = player.Backpack:GetChildren()
local Character = player.Character
if Character and tools then
if Character:FindFirstChild("HumanoidRootPart") then
local part = Instance.new("Part")
local toolfolder = Instance.new("Folder")
toolfolder.Name = `toolfolder_{player.Name}`
toolfolder.Parent = game.ServerStorage
for _, itens in tools do
itens.Parent = toolfolder
end
part.Name = "Loot"
part.CFrame = Character.HumanoidRootPart.CFrame
part.Parent = game.Workspace
local Proximity = Instance.new("ProximityPrompt")
Proximity.Parent = part
Proximity.Triggered:Connect(function(TheOneWhoTriggered)
if TheOneWhoTriggered.Name ~= player.Name then
PickupItens(TheOneWhoTriggered, toolfolder, part)
elseif TheOneWhoTriggered.Character then
local humanoid = TheOneWhoTriggered.Character:FindFirstChild("Humanoid")
if humanoid and humanoid.Health > 0 then
PickupItens(TheOneWhoTriggered, toolfolder, part)
end
end
end)
end
end
end)
LocalScript:
local Player = game:GetService("Players").LocalPlayer
local Char = Player.Character or Player.CharacterAdded:Wait()
Char:WaitForChild("Humanoid").Died:Connect(function()
game.ReplicatedStorage.RemoteEvent:FireServer()
end)