Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Scripts not being able to change parents of objects?

Asked by 2 years ago
Edited 2 years ago

Ok so I have a code where after players have loaded up into the map it changes their parent. In the same code I also have where it changes the items in their "inventory custom folder" to the actual ROBLOX inventory. Well you see moving the items into the custom inventory folder works and so does moving the players parent to a folder but moving them again does not why? It even prints out "Parent Changed" but when I go and check in sever mode it didnt change.

This is my code:

It has a ton of whitespace and useless code I know, but it WORKS. The important part is Line 67 and 82 - 88.

while true do
    wait (0.5)

    value = game.ServerStorage.Map.Players

    timer = game.ServerStorage.Map.Time

    if game.ServerStorage.Map.Players.Value == 1 then



        for i = 20,0,-1 do
            if value.Value >= 1 then
                    wait (1)
                timer.Value = i

            elseif value == 0 then
                timer.Value = 20

            end
                end      




    if game.ServerStorage.Map.Time.Value == 0 then
            print "LoadedMap"

            local themap = game.ServerStorage.Map.Build:Clone()

            themap.Parent = game.Workspace      

            tpposy = math.random(0,100000)

            tpposx = math.random(0,100000)

            tpposz = math.random(0,100000)

            themap.Part.Position = Vector3.new (tpposy,tpposx,tpposz)

            print (tpposx)

            wait (1)

        local players = game.Workspace.PlayersMap:GetChildren()

            for i = 1, #players do
                playerthing = players[i]
                rootpart = players[i].HumanoidRootPart
                playerparent = players[i].Parent

            wait (0.5)
            rootpart.CFrame = CFrame.new(tpposy,tpposx,tpposz)

                rootpart.CFrame = CFrame.new(tpposy,tpposx,tpposz)

                rootpart.CFrame = CFrame.new(tpposy,tpposx,tpposz)

                rootpart.CFrame = CFrame.new(tpposy,tpposx,tpposz)

                wait (1)





                playerparent = game.Workspace



                print "Parent Changed"

            game.ServerStorage.Map.Players.Value = 0



                    game.ServerStorage.Map.Closed.Value = true




                actualplayer = game.Players:GetPlayerFromCharacter(playerthing)

                        playerweapons = actualplayer.Inventory:GetChildren()

                        playerweapons.Parent = actualplayer.Backpack






                  end
              end
          end
      end

1 answer

Log in to vote
1
Answered by 2 years ago
Edited 2 years ago

This problem is quite common and a bit confusing at first, but it's quite easy to avoid once you learn how to solve it.

So what is happening is that you're setting a variable (in your case playerparent) to a property (in your case the parent) of an object, and when doing this the variable stores what the property was when the variable was set. So you're not storing a path to the property itself, but rather what it was when you set the variable.

This might sound a bit confusing so here's an example:

local part = workspace.Part -- A part

local anchored = part.Anchored -- You've now set the variable to what part.Anchored was (a true/false variable, also known as a boolean)

print(anchored) -- Let's say that the part isn't anchored, so it prints 'false'

anchored = true -- As we now know, anchored is a boolean (true/false) value, so we're actually just setting the value to true, rather than changing the property

print(anchored) -- Prints true, because we set the variable to true.

print(part.Anchored) -- Still prints false, since we set the variable and not the property.

Solving this is quite easy, since we're just storing a property of an object we can just reference the object itself and change the property that way. An example:

local part = workspace.Part -- A part

local anchored = part.Anchored -- You've now set the variable to what part.Anchored was (a true/false variable, also known as a boolean)

print(part.Anchored) -- Let's say that the part isn't anchored, so it prints 'false'

part.Anchored = true -- Now we're actually changing the property instead of the value

print(part.Anchored) -- prints true, since we anchored the part.

print(anchored) -- prints false, since we set the property to true but not the variable.

I hope those examples cleared a few things up, now here's your script but with the slight change:

while true do
    wait (0.5)
    value = game.ServerStorage.Map.Players
    timer = game.ServerStorage.Map.Time
    if game.ServerStorage.Map.Players.Value == 1 then
        for i = 20,0,-1 do
            if value.Value >= 1 then
                wait (1)
                timer.Value = i
            elseif value == 0 then
                timer.Value = 20
            end
        end      
        if game.ServerStorage.Map.Time.Value == 0 then
            print "LoadedMap"
            local themap = game.ServerStorage.Map.Build:Clone()
            themap.Parent = game.Workspace      
            tpposy = math.random(0,100000)
            tpposx = math.random(0,100000)
            tpposz = math.random(0,100000)
            themap.Part.Position = Vector3.new (tpposy,tpposx,tpposz)
            print (tpposx)
            wait (1)
            local players = game.Workspace.PlayersMap:GetChildren()
            for i = 1, #players do
                playerthing = players[i]
                rootpart = players[i].HumanoidRootPart
                playerparent = players[i].Parent
                wait (0.5)
                rootpart.CFrame = CFrame.new(tpposy,tpposx,tpposz)
                rootpart.CFrame = CFrame.new(tpposy,tpposx,tpposz)
                rootpart.CFrame = CFrame.new(tpposy,tpposx,tpposz)
                rootpart.CFrame = CFrame.new(tpposy,tpposx,tpposz)
                wait (1)
                player[i].Parent = game.Workspace -- Changed here
                print "Parent Changed"
                game.ServerStorage.Map.Players.Value = 0
                game.ServerStorage.Map.Closed.Value = true
                actualplayer = game.Players:GetPlayerFromCharacter(playerthing)
                playerweapons = actualplayer.Inventory:GetChildren()
                playerweapons.Parent = actualplayer.Backpack
            end
        end
    end
end
Ad

Answer this question