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

My code doesn't work, and I do not know what the problem is. help is very helpful.. Any help?

Lately, I have been working on a sword fight game. Though a line doesn't work and i am confused, any help would be appreciated.

Code:

--Defining variables

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local ServerStorage = game:GetService("ServerStorage")

local MapsFolder = ServerStorage:WaitForChild("Maps")

local Status = ReplicatedStorage:WaitForChild("Status")

local GameLenght = 50

local reward = 50

--game loop

while true do Status.Value = "Waiting for Players."

repeat wait(1) until game.Players.NumPlayers >= 2

Status.Value = "Intermission"

wait(10)

local plrs = {}

for i, player in pairs(game.Players:GetPlayers()) do
	if player then
		table.insert(plrs,player) --add each player into plrs table
	end
end

wait(2)

local AvailableMaps = MapsFolder:GetChildren()

local ChosenMap = AvailableMaps[math.random(1,#AvailableMaps)]

Status.Value = ChosenMap.Name.." Chosen!"

local ClonedMap = ChosenMap:Clone()
ClonedMap.Parent = workspace

--Teleport Players

local SpawnPoints = ClonedMap:FindFirstChild("SpawnPoints")

if not SpawnPoints then
	print("no")
end

local AvailableSpawnPoints = SpawnPoints:GetChildren()
for i, player in pairs(plrs) do
	if player then
		character = player.Character
		
		if character then
			--tp
			character:FindFirstChild("HumanoidRootPart").CFrame = AvailableSpawnPoints[1].CFrame
			table.remove(AvailableSpawnPoints,1)
			
			--sword giving!
			
			local sword = ServerStorage.ClassicSword:Clone()
			sword.Parent = player.Backpack
			
			local GameTag = Instance.new("BoolValue")
			GameTag.Name = "GameTag"
			GameTag.Parent = player.Character
		else
			--no char
			if not player then
				table.remove(plrs,i)
			end
			
		end
	end
end


Status.Value = "Get Ready TO BRAWL!"

wait(2)

for i = GameLenght,0,-1 do
	
	for x, player in pairs(plrs) do
		if player then
			
			character = player.Character
			
			if not character then
				--left game
			else
				if character:FindFirstChild("GameTag") then
					print(player.Name.." is still here. he/she has potential.")
				else
					--ded
					table.remove(plrs,x)
					print(player.Name.." Has been removed")
				end
			end
		else
			table.remove(plrs,x)
			print(player.Name.." Has been removed")
		end
	end
end

Status.Value = "There are "..i.." Seconds remaining, and "..#plrs.." players are left"

if #plrs == 1 then
	Status.Value = "THE WINNER IS... "..plrs[1].Name
	plrs[1].leaderstats.Bucks.Value = plrs[1].leaderstats.Bucks.Value + reward
	break
elseif #plrs == 0 then
	Status.Value = "EASTER EGG! HAHA!"
	break
elseif i == 0 then
	Status.Value = "Time's up. nobody won"
	
end

print("End of game")

for i, player in pairs(game.Players:GetPlayers()) do
	character = player.Character
	
	if not character then
		--ignore
	else
		if character:FindFirstChild("GameTag") then
			character.GameTag:Destroy()
		end
		
		if player.Backpack:FindFirstChild("ClassicSword") then
			player.Backpack.ClassicSword:Destroy()
		end
		
		if character:FindFirstChild("ClassicSword") then
			character.ClassicSword:Destroy()
		end
		
	end
	
	player:LoadCharacter()
end

ClonedMap:Destroy()

Status.Value = "Game...Ended."

end

it says that the "i" at the "there are ..i.. seconds remaining is wrong. any help to debug that?

4 comments

0

Help would be needed.

0

You are trying to modify the Status.Value AFTER you ended your loop. Move that line inbtween the ends on line 89 and 90

0

It worked, thank you, but there is the same problem (I think) at line 101. I have no idea where to put it though.

0

Nevermind.

1 answer

✓ Accepted answer

Sorry I should have looked over it better, didnt realize there was more to it. Basically everything from Status.Value to the end after line 101 needs to go inside the loop.

repeat wait(1) until game.Players.NumPlayers >= 2

Status.Value = "Intermission"

wait(10)

local plrs = {}

for i, player in pairs(game.Players:GetPlayers()) do
    if player then
        table.insert(plrs,player) --add each player into plrs table
    end
end

wait(2)

local AvailableMaps = MapsFolder:GetChildren()

local ChosenMap = AvailableMaps[math.random(1,#AvailableMaps)]

Status.Value = ChosenMap.Name.." Chosen!"

local ClonedMap = ChosenMap:Clone()
ClonedMap.Parent = workspace

--Teleport Players

local SpawnPoints = ClonedMap:FindFirstChild("SpawnPoints")

if not SpawnPoints then
    print("no")
end

local AvailableSpawnPoints = SpawnPoints:GetChildren()
for i, player in pairs(plrs) do
    if player then
        character = player.Character

        if character then
            --tp
            character:FindFirstChild("HumanoidRootPart").CFrame = AvailableSpawnPoints[1].CFrame
            table.remove(AvailableSpawnPoints,1)

            --sword giving!

            local sword = ServerStorage.ClassicSword:Clone()
            sword.Parent = player.Backpack

            local GameTag = Instance.new("BoolValue")
            GameTag.Name = "GameTag"
            GameTag.Parent = player.Character
        else
            --no char
            if not player then
                table.remove(plrs,i)
            end

        end
    end
end


Status.Value = "Get Ready TO BRAWL!"

wait(2)

for i = GameLenght,0,-1 do

    for x, player in pairs(plrs) do
        if player then

            character = player.Character

            if not character then
                --left game
            else
                if character:FindFirstChild("GameTag") then
                    print(player.Name.." is still here. he/she has potential.")
                else
                    --ded
                    table.remove(plrs,x)
                    print(player.Name.." Has been removed")
                end
            end
        else
            table.remove(plrs,x)
            print(player.Name.." Has been removed")
        end
    end
Status.Value = "There are "..i.." Seconds remaining, and "..#plrs.." players are left"

if #plrs == 1 then
    Status.Value = "THE WINNER IS... "..plrs[1].Name
    plrs[1].leaderstats.Bucks.Value = plrs[1].leaderstats.Bucks.Value + reward
    break
elseif #plrs == 0 then
    Status.Value = "EASTER EGG! HAHA!"
    break
elseif i == 0 then
    Status.Value = "Time's up. nobody won"

end

end


print("End of game")

for i, player in pairs(game.Players:GetPlayers()) do
    character = player.Character

    if not character then
        --ignore
    else
        if character:FindFirstChild("GameTag") then
            character.GameTag:Destroy()
        end

        if player.Backpack:FindFirstChild("ClassicSword") then
            player.Backpack.ClassicSword:Destroy()
        end

        if character:FindFirstChild("ClassicSword") then
            character.ClassicSword:Destroy()
        end

    end

    player:LoadCharacter()
end

ClonedMap:Destroy()

Status.Value = "Game...Ended."

3 comments

0

This was good, but right after the game starts the match ends. any help?

0

And when 1 player remains he stays there (probably know that. i will let you know

0

You need to add a wait() to your loop. Right after you start the loop ( for i = GameLenght,0,-1 do) add wait(1). That will wait 1 second inbetween the loop firing. As far as the character staying I am not sure

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