Jump to content

Team & Skin Selection


shaio

Recommended Posts

This is all server side. I have no errors and no warnings in debugscript. I cannot find what is wrong with this script. Could someone please help me? I'm using just 2 skins for test purposes. I cannot change team or skin.

local root = getRootElement()
defaultSkin = 0
defaultTeam = ""
currentSkin = 1
currentTeam = 1
team = ""
enabled = false

-------------------------------------------------- Configuration --------------------------------------------------
k = "F4"

teams = {
    {team = "Freeroamer",x = -1980.03125,y = 884.171875,z = 45.203125,camX = -1991.60546875,camY = 883.015625,camZ = 49.198482513428,rot = 0,r = 0,g = 0,b = 0},
    {team = "Gangster",x = 2498.328125,y = -1666.9169921875,z = 13.345584869385,camX = 2485.46484375,camY = -1666.2373046875,camZ = 16.649211883545,rot = 0,r = 0,g = 0,b = 0},
    {team = "Doctor",x = 1178.6708984375,y = -1323.6162109375,z = 14.131542205811,camX = 1178.6708984375,camY = -1323.6162109375,camZ = 13.531591415405,rot = 0,r = 0,g = 0,b = 0},
	{team = "Police",x = 1542.80078125,y = -1674.4306640625,z = 13.555210113525,camX = 1526.4638671875,camY = -1675.212890625,camZ = 19.493789672852,rot = 0,r = 0,g = 0,b = 0},
    {team = "Military",x = 201.4443359375,y = 1897.6630859375,z = 17.640625,camX = 204.458984375,camY = 1919.634765625,camZ = 23.877788543701,rot = 0,r = 0,g = 0,b = 0},
    {team = "Staff",x = -2025.8017578125,y = 171.3896484375,z = 28.84375,camX = -2002.76171875,camY = 170.9296875,camZ = 39.366466522217,rot = 0,r = 0,g = 0,b = 0}
}

skins = {}
skins["Freeroamer"] = {0,5}
skins["Gangster"] = {1,4}
skins["Doctor"] = {2,3}
skins["Police"] = {3,2}
skins["Military"] = {4,1}
skins["Staff"] = {5,0}
-------------------------------------------------------------------------------------------------------------------

addEventHandler("onPlayerLogin",root,function()
	setAccountData(getPlayerAccount(source),"firstTime",true)
	bindKey(source,k,"down",deathChangeTeam)
	firstChange(source)
end)

for i,t in pairs(teams) do
    createTeam(t.team,t.r,t.g,t.b)
end

function firstChange(player)
    if getElementType(player) == "player" then
        defaultSkin = getElementModel(player)
		if getPlayerTeam(player) then
			defaultTeam = getPlayerTeam(player)
		end
		team = "Freeroamer"
		if currentSkin == #skins[tostring(team)] then
            currentSkin = 1
        else
            currentSkin = currentSkin + 1
        end
		updateSkin(player)
		for i,te in pairs(teams) do
			if te.team == team then
				setElementPosition(player,tonumber(te.x),tonumber(te.y),tonumber(te.z))
				setCameraMatrix(player,tonumber(te.camX),tonumber(te.camY),tonumber(te.camZ),tonumber(te.x),tonumber(te.y),tonumber(te.z),0,70)
			end
		end
        setElementFrozen(player,true)
		bindKey(player,"arrow_u","down",changeTeam)
		bindKey(player,"arrow_d","down",changeTeam)
		bindKey(player,"arrow_l","down",changeSkin)
        bindKey(player,"arrow_r","down",changeSkin)
        bindKey(player,"enter","down",finishSelection)
		if getAccountData(getPlayerAccount(player),"firstTime") == false then
			bindKey(player,"backspace","down",exitSelection)
		end
		toggleAllControls(player,false)
    end
end

function changeSkin(player,key)
    if key == "arrow_l" then
        if currentSkin == 1 then
            currentSkin = #skins[tostring(team)]
        else
            currentSkin = currentSkin - 1
        end
   elseif key == "arrow_r" then
        if currentSkin == #skins[tostring(team)] then
            currentSkin = 1
        else
            currentSkin = currentSkin + 1
        end
    end
    updateSkin(player)
end

function changeTeam(player,key)
    if key == "arrow_d" then
        if currentTeam == 1 then
            currentTeam = #teams
        else
            currentTeam = currentTeam - 1
        end
   elseif key == "arrow_u" then
        if currentTeam == #teams then
            currentTeam = 1
        else
            currentTeam = currentTeam + 1
        end
    end
    updateTeam(player)
end

function updateSkin(player)
    for i,s in pairs(skins[tostring(team)]) do
        if i == currentSkin then
            setElementModel(player,tonumber(s))
        end
    end
end

function updateTeam(player)
    for i,t in pairs(teams) do
        if i == currentTeam then
			if t.team == team then
				team = t.team
				updateSkin(player)
				setElementPosition(player,tonumber(t.x),tonumber(t.y),tonumber(t.z))
				setCameraMatrix(player,tonumber(t.camX),tonumber(t.camY),tonumber(t.camZ),tonumber(t.x),tonumber(t.y),tonumber(t.z),0,70)
			end
        end
    end
end

function deathChangeTeam(player,key)
	if key == "f4" then
		addEventHandler("onPlayerWasted",root,function()
			if source == player then
				if getAccountData(getPlayerAccount(source),"firstTime") == false then
					if enabled == false then
						firstChange(source)
						outputChatBox("You will now be entered into team selection when you die.",player,0,255,255)
						enabled = true
					elseif enabled == true then
						outputChatBox("Team selection cancelled!",player,0,255,255)
						enabled = false
					end
				end
			end
		end)
	end
end

function finishSelection(player,key)
    if key == "enter" then
        setCameraTarget(player,player)
        setElementFrozen(player,false)
		toggleAllControls(player,true)
		setPlayerTeam(player,getTeamFromName(team))
        unbindKey(player,"arrow_u","down",changeTeam)
        unbindKey(player,"arrow_d","down",changeTeam)
        unbindKey(player,"arrow_l","down",changeSkin)
        unbindKey(player,"arrow_r","down",changeSkin)
        unbindKey(player,"enter","down",finishSelection)
        unbindKey(player,"backspace","down",exitSelection)
		setAccountData(getPlayerAccount(player),"firstTime",true)
    end
end

function exitSelection(player,key)
	if key == "backspace" then
        setElementModel(player,tonumber(defaultSkin))
		setPlayerTeam(player,defaultTeam)
        setCameraTarget(player,player)
        setElementFrozen(player,false)
		toggleAllControls(player,true)
        unbindKey(player,"arrow_u","down",changeTeam)
        unbindKey(player,"arrow_d","down",changeTeam)
        unbindKey(player,"arrow_l","down",changeSkin)
        unbindKey(player,"arrow_r","down",changeSkin)
        unbindKey(player,"enter","down",finishSelection)
        unbindKey(player,"backspace","down",exitSelection)
    end
end

 

Link to comment

Are you starting the resource when already connected?

Also there are parts i don't understand:

if currentSkin == #skins[tostring(team)] then
            currentSkin = 1

and currentskin is the model of the player... why compare to table lenght?

Edited by LoPollo
Link to comment

Are you starting the resource when already connected?

Also there are parts i don't understand:


if currentSkin == #skins[tostring(team)] then
            currentSkin = 1

and currentskin is the model of the player... why compare to table lenght?

I want to be able to change skins, and go in order with the table index.

Edited by shaio
Link to comment

I was trying to understand the code but i gave up since i have no much time right now... there's client logic inside a server side script... i'm talking about the first variables like currentSkin, currentTeam... 

also you are forcing with tostring something that will be for sure a string (you set it in the script and it'is not touched otherwise, so if you set it only as string it will be always a string O.o )

also rename some variables, like "k", i think stands for key... but k is a common variable in for loops, and even if that's not the case this practice can lead to errors. And me to not understand xD

other variables to rename are team and player.

 

Another thing to be aware is that in deathChangeTeam you are adding an handler, which will be added and added and added and added, everytime the function is called. This handler is also bound to root, but you are using "if source == player then" so what about binding it directly to what you need to check? (the player)

I think it's better to rewrite some parts of the script, using tables when needed

Ignore below, it's just the script correctly tabbed with the very first questions that passed in my mind...

Spoiler

Ignore, i posted it just cause originally i wanted to ask these question, but since i now think that a code rewrite would be better, it's here only to give an idea about what i thought


local root = getRootElement()
defaultSkin = 0
defaultTeam = ""
currentSkin = 1
currentTeam = 1
team = ""
enabled = false

-------------------------------------------------- Configuration --------------------------------------------------
k = "F4"

teams = {
	{team = "Freeroamer",x = -1980.03125,y = 884.171875,z = 45.203125,camX = -1991.60546875,camY = 883.015625,camZ = 49.198482513428,rot = 0,r = 0,g = 0,b = 0},
	{team = "Gangster",x = 2498.328125,y = -1666.9169921875,z = 13.345584869385,camX = 2485.46484375,camY = -1666.2373046875,camZ = 16.649211883545,rot = 0,r = 0,g = 0,b = 0},
	{team = "Doctor",x = 1178.6708984375,y = -1323.6162109375,z = 14.131542205811,camX = 1178.6708984375,camY = -1323.6162109375,camZ = 13.531591415405,rot = 0,r = 0,g = 0,b = 0},
	{team = "Police",x = 1542.80078125,y = -1674.4306640625,z = 13.555210113525,camX = 1526.4638671875,camY = -1675.212890625,camZ = 19.493789672852,rot = 0,r = 0,g = 0,b = 0},
	{team = "Military",x = 201.4443359375,y = 1897.6630859375,z = 17.640625,camX = 204.458984375,camY = 1919.634765625,camZ = 23.877788543701,rot = 0,r = 0,g = 0,b = 0},
	{team = "Staff",x = -2025.8017578125,y = 171.3896484375,z = 28.84375,camX = -2002.76171875,camY = 170.9296875,camZ = 39.366466522217,rot = 0,r = 0,g = 0,b = 0}
}

skins = {}
skins["Freeroamer"] = {0,5}
skins["Gangster"] = {1,4}
skins["Doctor"] = {2,3}
skins["Police"] = {3,2}
skins["Military"] = {4,1}
skins["Staff"] = {5,0}

for i,t in pairs(teams) do
	createTeam(t.team,t.r,t.g,t.b)
end

-------------------------------------------------------------------------------------------------------------------

addEventHandler("onPlayerLogin",root,
	function()
		setAccountData(getPlayerAccount(source),"firstTime",true) --firstTime is too generic, append the res name to differentiate: getResourceName ( resource ).."_firstTime"
		bindKey(source,k,"down",deathChangeTeam)
		firstChange(source)
	end
)

function firstChange(player)
	if getElementType(player) == "player" then
		defaultSkin = getElementModel(player) --default skin is for all players, it should be a table
		if getPlayerTeam(player) then
			defaultTeam = getPlayerTeam(player)
		end
		team = "Freeroamer" --same as above

		--i still don't understand this: could you explain what this should do?
		if currentSkin == #skins[tostring(team)] then 
			currentSkin = 1
		else
			currentSkin = currentSkin + 1
		end
		------------------------------------------------------------------------

		updateSkin(player)

		for i,te in pairs(teams) do
			if te.team == team then
				setElementPosition(player,tonumber(te.x),tonumber(te.y),tonumber(te.z))
				setCameraMatrix(player,tonumber(te.camX),tonumber(te.camY),tonumber(te.camZ),tonumber(te.x),tonumber(te.y),tonumber(te.z),0,70)
			end
		end

		setElementFrozen(player,true)
		bindKey(player,"arrow_u","down",changeTeam)
		bindKey(player,"arrow_d","down",changeTeam)
		bindKey(player,"arrow_l","down",changeSkin)
		bindKey(player,"arrow_r","down",changeSkin)
		bindKey(player,"enter","down",finishSelection)
		
		if not getAccountData(getPlayerAccount(player),"firstTime") then
			bindKey(player,"backspace","down",exitSelection)
		end

		toggleAllControls(player,false)
	end
end

function changeSkin(player,key)
	if key == "arrow_l" then
		if currentSkin == 1 then
			currentSkin = #skins[tostring(team)]
		else
			currentSkin = currentSkin - 1
		end
	elseif key == "arrow_r" then
		if currentSkin == #skins[tostring(team)] then
			currentSkin = 1
		else
			currentSkin = currentSkin + 1
		end
	end
	updateSkin(player)
end

function changeTeam(player,key)
	if key == "arrow_d" then
		if currentTeam == 1 then
			currentTeam = #teams
		else
			currentTeam = currentTeam - 1
		end
	elseif key == "arrow_u" then
		if currentTeam == #teams then
			currentTeam = 1
		else
			currentTeam = currentTeam + 1
		end
	end
	updateTeam(player)
end

function updateSkin(player)
	for i,s in pairs(skins[tostring(team)]) do
		if i == currentSkin then -- don't understand this also
			setElementModel(player,tonumber(s))
		end
	end
end

function updateTeam(player)
	for i,t in pairs(teams) do
		if i == currentTeam then
			if t.team == team then
				team = t.team
				updateSkin(player)
				setElementPosition(player,tonumber(t.x),tonumber(t.y),tonumber(t.z))
				setCameraMatrix(player,tonumber(t.camX),tonumber(t.camY),tonumber(t.camZ),tonumber(t.x),tonumber(t.y),tonumber(t.z),0,70)
			end
		end
	end
end

function deathChangeTeam(player,key)
	if key == "f4" then
		addEventHandler("onPlayerWasted",root,
			function()
				if source == player then
					if not getAccountData(getPlayerAccount(source),"firstTime") then
						if not enabled then
							firstChange(source)
							outputChatBox("You will now be entered into team selection when you die.",player,0,255,255)
							enabled = true
						elseif enabled then
							outputChatBox("Team selection cancelled!",player,0,255,255)
							enabled = false
						end
					end
				end
			end
		)
	end
end

function finishSelection(player,key)
	if key == "enter" then
		setCameraTarget(player,player)
		setElementFrozen(player,false)
		toggleAllControls(player,true)
		setPlayerTeam(player,getTeamFromName(team))
		unbindKey(player,"arrow_u","down",changeTeam)
		unbindKey(player,"arrow_d","down",changeTeam)
		unbindKey(player,"arrow_l","down",changeSkin)
		unbindKey(player,"arrow_r","down",changeSkin)
		unbindKey(player,"enter","down",finishSelection)
		unbindKey(player,"backspace","down",exitSelection)
		setAccountData(getPlayerAccount(player),"firstTime",true)
	end
end

function exitSelection(player,key)
	if key == "backspace" then
		setElementModel(player,tonumber(defaultSkin))
		setPlayerTeam(player,defaultTeam)
		setCameraTarget(player,player)
		setElementFrozen(player,false)
		toggleAllControls(player,true)
		unbindKey(player,"arrow_u","down",changeTeam)
		unbindKey(player,"arrow_d","down",changeTeam)
		unbindKey(player,"arrow_l","down",changeSkin)
		unbindKey(player,"arrow_r","down",changeSkin)
		unbindKey(player,"enter","down",finishSelection)
		unbindKey(player,"backspace","down",exitSelection)
	end
end

 

Link to comment
On 11/19/2016 at 6:12 AM, LoPollo said:

I was trying to understand the code but i gave up since i have no much time right now... there's client logic inside a server side script... i'm talking about the first variables like currentSkin, currentTeam... 

also you are forcing with tostring something that will be for sure a string (you set it in the script and it'is not touched otherwise, so if you set it only as string it will be always a string O.o )

also rename some variables, like "k", i think stands for key... but k is a common variable in for loops, and even if that's not the case this practice can lead to errors. And me to not understand xD

other variables to rename are team and player.

 

Another thing to be aware is that in deathChangeTeam you are adding an handler, which will be added and added and added and added, everytime the function is called. This handler is also bound to root, but you are using "if source == player then" so what about binding it directly to what you need to check? (the player)

I think it's better to rewrite some parts of the script, using tables when needed

Ignore below, it's just the script correctly tabbed with the very first questions that passed in my mind...

  Hide contents

Ignore, i posted it just cause originally i wanted to ask these question, but since i now think that a code rewrite would be better, it's here only to give an idea about what i thought



 local root = getRootElement()defaultSkin = 0defaultTeam = ""currentSkin = 1currentTeam = 1team = ""enabled = false-------------------------------------------------- Configuration --------------------------------------------------k = "F4"teams = {	{team = "Freeroamer",x = -1980.03125,y = 884.171875,z = 45.203125,camX = -1991.60546875,camY = 883.015625,camZ = 49.198482513428,rot = 0,r = 0,g = 0,b = 0},	{team = "Gangster",x = 2498.328125,y = -1666.9169921875,z = 13.345584869385,camX = 2485.46484375,camY = -1666.2373046875,camZ = 16.649211883545,rot = 0,r = 0,g = 0,b = 0},	{team = "Doctor",x = 1178.6708984375,y = -1323.6162109375,z = 14.131542205811,camX = 1178.6708984375,camY = -1323.6162109375,camZ = 13.531591415405,rot = 0,r = 0,g = 0,b = 0},	{team = "Police",x = 1542.80078125,y = -1674.4306640625,z = 13.555210113525,camX = 1526.4638671875,camY = -1675.212890625,camZ = 19.493789672852,rot = 0,r = 0,g = 0,b = 0},	{team = "Military",x = 201.4443359375,y = 1897.6630859375,z = 17.640625,camX = 204.458984375,camY = 1919.634765625,camZ = 23.877788543701,rot = 0,r = 0,g = 0,b = 0},	{team = "Staff",x = -2025.8017578125,y = 171.3896484375,z = 28.84375,camX = -2002.76171875,camY = 170.9296875,camZ = 39.366466522217,rot = 0,r = 0,g = 0,b = 0}}skins = {}skins["Freeroamer"] = {0,5}skins["Gangster"] = {1,4}skins["Doctor"] = {2,3}skins["Police"] = {3,2}skins["Military"] = {4,1}skins["Staff"] = {5,0}for i,t in pairs(teams) do	createTeam(t.team,t.r,t.g,t.b)end-------------------------------------------------------------------------------------------------------------------addEventHandler("onPlayerLogin",root,	function()		setAccountData(getPlayerAccount(source),"firstTime",true) --firstTime is too generic, append the res name to differentiate: getResourceName ( resource ).."_firstTime"		bindKey(source,k,"down",deathChangeTeam)		firstChange(source)	end)function firstChange(player)	if getElementType(player) == "player" then		defaultSkin = getElementModel(player) --default skin is for all players, it should be a table		if getPlayerTeam(player) then			defaultTeam = getPlayerTeam(player)		end		team = "Freeroamer" --same as above		--i still don't understand this: could you explain what this should do?		if currentSkin == #skins[tostring(team)] then 			currentSkin = 1		else			currentSkin = currentSkin + 1		end		------------------------------------------------------------------------		updateSkin(player)		for i,te in pairs(teams) do			if te.team == team then				setElementPosition(player,tonumber(te.x),tonumber(te.y),tonumber(te.z))				setCameraMatrix(player,tonumber(te.camX),tonumber(te.camY),tonumber(te.camZ),tonumber(te.x),tonumber(te.y),tonumber(te.z),0,70)			end		end		setElementFrozen(player,true)		bindKey(player,"arrow_u","down",changeTeam)		bindKey(player,"arrow_d","down",changeTeam)		bindKey(player,"arrow_l","down",changeSkin)		bindKey(player,"arrow_r","down",changeSkin)		bindKey(player,"enter","down",finishSelection)				if not getAccountData(getPlayerAccount(player),"firstTime") then			bindKey(player,"backspace","down",exitSelection)		end		toggleAllControls(player,false)	endendfunction changeSkin(player,key)	if key == "arrow_l" then		if currentSkin == 1 then			currentSkin = #skins[tostring(team)]		else			currentSkin = currentSkin - 1		end	elseif key == "arrow_r" then		if currentSkin == #skins[tostring(team)] then			currentSkin = 1		else			currentSkin = currentSkin + 1		end	end	updateSkin(player)endfunction changeTeam(player,key)	if key == "arrow_d" then		if currentTeam == 1 then			currentTeam = #teams		else			currentTeam = currentTeam - 1		end	elseif key == "arrow_u" then		if currentTeam == #teams then			currentTeam = 1		else			currentTeam = currentTeam + 1		end	end	updateTeam(player)endfunction updateSkin(player)	for i,s in pairs(skins[tostring(team)]) do		if i == currentSkin then -- don't understand this also			setElementModel(player,tonumber(s))		end	endendfunction updateTeam(player)	for i,t in pairs(teams) do		if i == currentTeam then			if t.team == team then				team = t.team				updateSkin(player)				setElementPosition(player,tonumber(t.x),tonumber(t.y),tonumber(t.z))				setCameraMatrix(player,tonumber(t.camX),tonumber(t.camY),tonumber(t.camZ),tonumber(t.x),tonumber(t.y),tonumber(t.z),0,70)			end		end	endendfunction deathChangeTeam(player,key)	if key == "f4" then		addEventHandler("onPlayerWasted",root,			function()				if source == player then					if not getAccountData(getPlayerAccount(source),"firstTime") then						if not enabled then							firstChange(source)							outputChatBox("You will now be entered into team selection when you die.",player,0,255,255)							enabled = true						elseif enabled then							outputChatBox("Team selection cancelled!",player,0,255,255)							enabled = false						end					end				end			end		)	endendfunction finishSelection(player,key)	if key == "enter" then		setCameraTarget(player,player)		setElementFrozen(player,false)		toggleAllControls(player,true)		setPlayerTeam(player,getTeamFromName(team))		unbindKey(player,"arrow_u","down",changeTeam)		unbindKey(player,"arrow_d","down",changeTeam)		unbindKey(player,"arrow_l","down",changeSkin)		unbindKey(player,"arrow_r","down",changeSkin)		unbindKey(player,"enter","down",finishSelection)		unbindKey(player,"backspace","down",exitSelection)		setAccountData(getPlayerAccount(player),"firstTime",true)	endendfunction exitSelection(player,key)	if key == "backspace" then		setElementModel(player,tonumber(defaultSkin))		setPlayerTeam(player,defaultTeam)		setCameraTarget(player,player)		setElementFrozen(player,false)		toggleAllControls(player,true)		unbindKey(player,"arrow_u","down",changeTeam)		unbindKey(player,"arrow_d","down",changeTeam)		unbindKey(player,"arrow_l","down",changeSkin)		unbindKey(player,"arrow_r","down",changeSkin)		unbindKey(player,"enter","down",finishSelection)		unbindKey(player,"backspace","down",exitSelection)	endend

The code works. It's just the two functions  updateTeam and updateSkin that wont run..

 

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...