for some reason when I trigger this function it does’nt work it will run even if the owner value is == nil orit will get multiple backpacks to random parts even if theres one player this is how its supposed to be
a random parts owner value gets changed to the players name and the same for the backpack and that for all players, btu it will just get multiple backpack or random backpacks I think it neven gets the good one did I do something wrong please help me!
task.wait(5)
local players = game.Players:GetChildren()
local parts = game:WaitForChild("Workspace").HouseMap.Backpacks.BackpackParts:GetChildren()
for _, player in players do
local randomPart = table.remove(parts, math.random(#parts))
randomPart.Owner.Value = player.Name
end
local backpacks = game:WaitForChild("Workspace").HouseMap.Backpacks.Backpacks:GetChildren()
for _, player in players do
local randomBackpack = table.remove(backpacks, math.random(#backpacks))
randomBackpack.Owner.Value = player.Name
end
game.ReplicatedStorage.Values.StoryValues.GetBackpacks:GetPropertyChangedSignal("Value"):Connect(function()
print("triggered")
for i,v in pairs(game:WaitForChild("Workspace").HouseMap.Backpacks.BackpackParts:GetChildren()) do
print("trou parts")
if v.Owner.Value ~= nil then
print("if owner nil")
local owner = v.Owner.Value
print(owner)
local partPos = v.CFrame
for i,v in pairs(game:WaitForChild("Workspace").HouseMap.Backpacks.Backpacks:GetChildren()) do
if v.Owner.Value == owner then
v.CFrame = partPos
end
end
end
end
end)
btw the wait is just so the plaeyr is loaded into the game.
What is a backpack? Is it like a model that gets attached to a player’s character? And could you post a screenshot of the whole game.Workspace.HouseMap.Backpacks instance hiearchy?
I’m a bit confused why you have two loops at the top of the script, and I can’t tell what the GetBackpacks.Changed listener is supposed to do, please explain it.
I think the issue is because you are duplicating variables in the loops in the getPropertyChangedSignal connection.
Try this:
task.wait(5)
local players = game.Players:GetChildren()
local parts = game:WaitForChild("Workspace").HouseMap.Backpacks.BackpackParts:GetChildren()
for _, player in players do
local randomPart = table.remove(parts, math.random(#parts))
randomPart.Owner.Value = player.Name
end
local backpacks = game:WaitForChild("Workspace").HouseMap.Backpacks.Backpacks:GetChildren()
for _, player in players do
local randomBackpack = table.remove(backpacks, math.random(#backpacks))
randomBackpack.Owner.Value = player.Name
end
game.ReplicatedStorage.Values.StoryValues.GetBackpacks:GetPropertyChangedSignal("Value"):Connect(function()
print("triggered")
for _,part in pairs(game:WaitForChild("Workspace").HouseMap.Backpacks.BackpackParts:GetChildren()) do
if part.Owner.Value ~= nil and part.Owner.Value ~= "" then --Edited logic here.
local partPos = part.CFrame
for _,backpack in ipairs(game.Workspace.HouseMap.Backpacks.Backpacks:GetChildren()) do
if backpack.Owner.Value == part.Owner.Value then
backpack.CFrame = partPos
print("PartOwner is "..part.Owner.Value.."and BackpackOwner is "..backpack.Owner.Value)
end
end
end
end
end)