Jump to content

[HELP] Problem with spawning players in teams with table


Dziugasc

Recommended Posts

Hello, I am dealing with this problem for 5+ hours, basically it works without team 100%, but with teams only 1st line in table teams works, if for example if I remove Truckers, only Police works and so on.

local teams = {
    --{"team",x,y,z,rotation,skinid, weapon1, weapon2, weapon3}
    {"Truckers",-2057.91650, -236.21437, 35.32031,0,206,22,25,5},
    {"Police",-1636.95337, 691.31042, 7.16481,0,280,22,25,3},
    {"Mafia",-2306.69556, 277.60596, 35.36875,0,126,30,28,24}
}

local guestSpawns = {
    --{x,y,z,rotation,skinid}
    {-1794.5,1212.2,32.7,0}, 
    {-2519.6001, 1216, 37.4, 0},
    {-1645, 1202.4, 32.9, 0},
    {-1592.9, 1271.6, 7.2, 0},
    {-1481.1, 686, 1.3, 0},
    {-1747.5, 210.89999, 3.6, 0},
    {-2031.1, -52.7, 35.4, 0},
    {-2678.2, -280.10001, 7.2, 0},
    {-2582.7, 319.5, 5.2, 0},
    {-2749.3994, -48.90039, 6.8, 0},
    {-2443.1001, 752, 35.2, 0},
    {-2154.1001, -421.70001, 35.3, 0},
    {-2238.8999, 112.5, 35.3, 0},
    {-2197.3999, 288.39999, 35.3, 0}
}

function getRandomSpawnpoint()
    randomNum = math.random(#guestSpawns)
    return randomNum
end

function spawnOnWasted()
cancelEvent()
fadeCamera(source, true)
local num = getRandomSpawnpoint()
local ammo = 1000
	local playerTeam = getPlayerTeam(source) -- source is the player who died onPlayerWasted
    local teamName = getTeamName(playerTeam) -- we get the team name
     for _, v in pairs(teams) do
        if teamName == v[1] then
            local thePlayer = source
            return setTimer(function() -- we return this
                spawnPlayer(thePlayer ,v[2],v[3],v[4],v[5],v[6],0,0,playerTeam)
                giveWeapon(thePlayer , v[7],ammo,false)
				giveWeapon(thePlayer , v[8],ammo,false)
				giveWeapon(thePlayer , v[9],ammo,false)
            end,2000,1)
        -- if it doesn't match (the player has no team or not specified team inside team table
        else
            for i,source in ipairs(getElementsByType("player")) do
        return setTimer(function()
            spawnPlayer(source,guestSpawns[num][1],guestSpawns[num][2],guestSpawns[num][3],guestSpawns[num][4],guestSpawns[num][5],0,0,playerTeam)
        --    giveWeapon(source, weapon,ammo,false)
        end,2000,1)
    end
        end
    end
	-- if player is in team but doesnt have spawnpoint use general spawnpoint
end
addEventHandler("onPlayerWasted", root,spawnOnWasted)

 

Link to comment
  • Moderators
6 hours ago, Dziugasc said:

if for example if I remove Truckers, only Police works and so on.

That is because the `return` keyword stops the whole function.

 

You are currently executing the following code, while I left most of the details out of it. How many times do you think this loop will run? (no matter what outcome of math.random is)

for i=1, 1000 do
	if math.random(2) == 1 then
		return true
	else
		return false
	end
end

A : 1000 times

B : 0 times

C : 1 time

Spoiler

C 1 time

 

 

 

Try to move the NOT state (as in no team has been found), after the loop.

--[[ ... ]]

local thePlayer = source

for _, v in pairs(teams) do
	if teamName == v[1] then
	
		setTimer(function()
			spawnPlayer(thePlayer ,v[2],v[3],v[4],v[5],v[6],0,0,playerTeam)
			giveWeapon(thePlayer , v[7],ammo,false)
			giveWeapon(thePlayer , v[8],ammo,false)
			giveWeapon(thePlayer , v[9],ammo,false)
		end,2000,1)
		
		return -- this stops the whole function...
	end
end


setTimer(function()
	spawnPlayer(thePlayer,guestSpawns[num][1],guestSpawns[num][2],guestSpawns[num][3],guestSpawns[num][4],guestSpawns[num][5],0,0,playerTeam)
end,2000,1)



--[[ ... ]]

 

By the way, this is just 1 out of many solutions.

 

 

Edited by IIYAMA
  • Thanks 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...