So when a player joins the game they get all their weapons that they had before they left, but when they reset their weapons duplicate. Heres what I tried
local M = Instance.new("Model")
M.Name = "WeaponsHolder"
M.Parent = workspace
function onHumanoidDied(h,player)
local wepHolder = Instance.new("Model") --
wepHolder.Parent = workspace.WeaponsHolder
wepHolder.Name = player.Name
local playerWeps = player.Backpack:GetChildren()
for i = 1, #playerWeps do
if game.StarterPack:findFirstChild(playerWeps[i].Name) == nil then
local wepValue = Instance.new("ObjectValue")
wepValue.Value = game.Lighting:findFirstChild(playerWeps[i].Name)
wepValue.Name = "WeaponValue"
wepValue.Parent = wepHolder
end
end
local playerTools = player.Character:children()
for i = 1, #playerTools do
if playerTools[i].className == "Tool" then
local toolValue = Instance.new("ObjectValue")
toolValue.Value = game.Lighting:findFirstChild(playerTools[i].Name)
toolValue.Name = "ToolValue"
toolValue.Parent = wepHolder
end
end
end
function onPlayerRespawn(property, player)
if property == "Character" and player.Character ~= nil then
local humanoid = player.Character.Humanoid
local p = player
local h = humanoid
if workspace.WeaponsHolder:findFirstChild(player.Name) ~= nil then
local itemValues = workspace.WeaponsHolder:findFirstChild(player.Name):children()
for i = 1, #itemValues do
itemValues[i].Value:clone().Parent = player.Backpack
end
workspace.WeaponsHolder:findFirstChild(player.Name):remove()
end
humanoid.Died:connect(function() onHumanoidDied(h, p) end )
end
end
function onPlayerEntered(newPlayer)
while true do
if newPlayer.Character ~= nil then break end
wait(5)
end
local humanoid = newPlayer.Character.Humanoid
humanoid.Died:connect(function() onHumanoidDied(humanoid, newPlayer) end )
newPlayer.Changed:connect(function(property) onPlayerRespawn(property, newPlayer) end )
end
game.Players.ChildAdded:connect(onPlayerEntered)
That saves their weapons on death and I put it into workspace.
local ds = game:GetService("DataStoreService"):GetDataStore("ToolSave")
game.Players.PlayerAdded:connect(function(plr)
local key = "id-"..plr.userId
pcall(function()
local tools = ds:GetAsync(key)
if tools then
for i,v in pairs(tools) do
local tool = game.ServerStorage.Tools:FindFirstChild(v)
if tool then
tool:Clone().Parent = plr:WaitForChild("Backpack")
tool:clone().Parent = plr:WaitForChild("StarterGear")
end
end
end
end)
end)
game.Players.PlayerRemoving:connect(function(plr)
local key = "id-"..plr.userId
pcall(function()
local toolsToSave = {}
for i,v in pairs(plr.Backpack:GetChildren()) do
if v then
table.insert(toolsToSave,v.Name)
end
end
ds:SetAsync(key,toolsToSave)
end)
end)
This is supposed to collect thier weapons into a folder which is in server storage, and then when they rejoin they get thier weapons back. But when they get their weapons if they reset the weapons duplicate. Can somebody pls help me fix this.
