Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is this script that moves models from one parent to another not working?

This script is supposed to detect if a player is one of the usernames mentioned in a table. If the player is that username, they can move maps from ServerStorage into Workspace. However, the script isn't working, and I have no idea why, since output gave me nothing.

Can someone please help me?

Here's the script:

current_map = nil

names = {"CoolJohnnyboy","Player1"} 

game.Players.PlayerAdded:connect(function(player)
	for i, v in pairs(names) do
		if v == player.Name then 
			player.Chatted:connect(function(chatmsg)
				if chatmsg:sub(1,6) == "!load/" then 
					local map = chatmsg:sub(7, chatmsg:len())
					if game.Lighting:FindFirstChild(map) then
						local mapp = game.ServerStorage.Maps:FindFirstChild("Urban"):Clone()
						if current_map ~= nil then
							game.Workspace[current_map]:Destroy()
						end
						mapp.Parent = game.Workspace
						current_map = mapp.Name
					end
				end
			end)
		end
	end
end)

4 comments

0

Put a print right before player.Chatted, and tell me if it prints

0

I did that, did not print anything.

0

Comment the if statement and try again. The problem could be there.

0

Obviously you will need it but to see if that is what is causing the problem...

1 answer

✓ Accepted answer

I'm not quite sure if you can call in pairs on a table, but you could call ipairs. I still think i=1 is much easier, so try this.

current_map = nil

names = {"CoolJohnnyboy","Player1"} 

game.Players.PlayerAdded:connect(function(player)
    for i = 1, #names do
        if names[i] == player.Name then 
            player.Chatted:connect(function(chatmsg)
                if chatmsg:sub(1,6) == "!load/" then 
                    local map = chatmsg:sub(7, chatmsg:len())
                    if game.Lighting:FindFirstChild(map) then
                        local mapp = game.ServerStorage.Maps:FindFirstChild("Urban"):Clone()
                        if current_map ~= nil then
                            game.Workspace[current_map]:Destroy()
                        end
                        mapp.Parent = game.Workspace
                        current_map = mapp.Name
                    end
                end
            end)
        end
    end
end)

This goes through the entire table and uses each values to compare to the player.

1 comment

0

Thanks! It Works!

This discussion is preserved as it was. New answers, comments, and votes are closed.