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

My button script will not work properly. Could someone help..?

My button script has a function that will teleport players if it has spawn points in it. But, it only teleports one player, help..?

Here's the script:




local db = true
local module = require(game.ServerStorage.Data)
script.Parent.MouseClick:Connect(function()
	if db == true then
		db = false
		for i, player in pairs(game.Players:GetPlayers()) do
			if player then
				local plrs = {}
				table.insert(plrs,player)
				local maps = game.ServerStorage.Maps
				local availableMaps = maps:GetChildren()
				local chosenMap = availableMaps[math.random(1,#availableMaps)]
				local clonedMap = chosenMap:Clone()
				game.Workspace.Intermission:Stop()
				clonedMap.Parent = workspace
				if clonedMap.Name == "Nebula" then
					game.Workspace.Part.Transparency = 1
				else
					--// Do nothing
				end
				if clonedMap:FindFirstChild("SpawnPoints") then
					local spawnPoints = clonedMap.SpawnPoints
					local availableSpawns = spawnPoints:GetChildren()
					player.Character.HumanoidRootPart.CFrame = availableSpawns[1].CFrame
					table.remove(availableSpawns,1)
				else
					--// Do nothing
				end
				if clonedMap:FindFirstChild("SkyBox") then
					clonedMap.SkyBox.Parent = game.Lighting
				end
				script.Parent.Parent.BrickColor = BrickColor.new("Bright red")
				wait(module.ButtonTime)
				player.Values.Points.Value = player.Values.Points.Value  + 100
				if game.Lighting:FindFirstChild("SkyBox") then
					game.Lighting.SkyBox:Destroy()
				end
				game.Workspace.Intermission:Play()
				script.Parent.Parent.BrickColor = BrickColor.new("Bright green")
				if clonedMap:FindFirstChild("SpawnPoints") then
					player:LoadCharacter()
				end
				if game.Workspace.Part.Transparency == 1 then
					game.Workspace.Part.Transparency = 0
				end
				clonedMap:Destroy()
				db = true
			end
		end
	end
end)



2 comments

0

You know, you try to copy the Map for each player?

0

what do you mean?

1 answer

When using "for i,v in pairs" you do this "for i,v in pairs (game.Players:GetChildren()) do" instead of "for i,v in pairs (game.Players:GetPlayers()) do", GetPlayers is glitchy for in pairs. I also fixed a couple things that made no since in the script like "if player then", And another thing that YOU need to fix cause its YOUR game. Every single time someone spawns in. It will copy the map for each player. So You may have some mass lag and clipping

local db = true
local module = require(game.ServerStorage.Data)
script.Parent.MouseClick:Connect(function()
    if db == true then
        db = false
        for i, player in pairs(game.Players:GetChildren()) do
                local plrs = {}
                table.insert(plrs,player)
                local maps = game.ServerStorage.Maps
                local availableMaps = maps:GetChildren()
                local chosenMap = availableMaps[math.random(1,#availableMaps)]
                local clonedMap = chosenMap:Clone()
                game.Workspace.Intermission:Stop()
                clonedMap.Parent = workspace
                if clonedMap.Name == "Nebula" then
                    game.Workspace.Part.Transparency = 1
                else
                    --// Do nothing
                end
                if clonedMap:FindFirstChild("SpawnPoints") then
                    local spawnPoints = clonedMap.SpawnPoints
                    local availableSpawns = spawnPoints:GetChildren()
                    player.Character.HumanoidRootPart.CFrame = availableSpawns[1].CFrame
                    table.remove(availableSpawns,1)
                else
                    --// Do nothing
                end
                if clonedMap:FindFirstChild("SkyBox") then
                    clonedMap.SkyBox.Parent = game.Lighting
                end
                script.Parent.Parent.BrickColor = BrickColor.new("Bright red")
                wait(module.ButtonTime)
                player.Values.Points.Value = player.Values.Points.Value  + 100
                if game.Lighting:FindFirstChild("SkyBox") then
                    game.Lighting.SkyBox:Destroy()
                end
                game.Workspace.Intermission:Play()
                script.Parent.Parent.BrickColor = BrickColor.new("Bright green")
                if clonedMap:FindFirstChild("SpawnPoints") then
                    player:LoadCharacter()
                end
                if game.Workspace.Part.Transparency == 1 then
                    game.Workspace.Part.Transparency = 0
                end
                clonedMap:Destroy()
                db = true
        end
    end
end)

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