Function and for loop acting weird!

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.

6 Likes

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.

2 Likes

heres a picture of the workspace.
Schermopname (38)

3 Likes

name your stuff better the moment you start doing .backpacks.backpacks.backpack youre making your life 150% more difficult

1 Like

Cool :+1: I still need the questions answered to be able to help tho

1 Like

its just a backpack on the ground witch needs to get teleported to the part

2 Likes

Oh, are you trying to move one backpack to one backpack “spawn”?

2 Likes

yes__________________ADSASDASDASD

2 Likes

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)
1 Like

Thanks, but al the backpacks are still teleporting even tho I only want that the ones with a value in the owner value to teleport can you do that?

1 Like

I edited the previous code, I think it was checking empty args and still returning true.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.