Jump to content

skin save on death bug


Bean666

Recommended Posts

addEventHandler ( "onPlayerWasted", root,
	function ( )
	    playerSkin = tonumber(getElementModel(source)) 
		if ( not playerWeapons [ source ] ) then
			playerWeapons [ source ] = { }
		end
		for slot = 0, 12 do
			local weapon = getPedWeapon ( source, slot )
			if ( weapon > 0 ) then
				local ammo = getPedTotalAmmo ( source, slot )
				if ( ammo > 0 ) then
					if (weapon == 37) then 
						ammo = ammo/10
					end
					playerWeapons [ source ] [ weapon ] = ammo
				end
			end
		end
	end
)

addEventHandler ( "onPlayerSpawn", root,
	function ( )
		if ( playerWeapons [ source ] ) then
		    if getElementData(source, "selecting") == nil then
		        setElementModel(source, playerSkin) 
			for weapon, ammo in pairs ( playerWeapons [ source ] ) do
				giveWeapon ( source, tonumber ( weapon ), tonumber ( ammo ) )
			end
			end
		end
		playerWeapons [ source ] = nil
	end
)

This code has save weapons, including skin on death, then load on spawn, they work perfectly, but there's one issue, when both players like die together, when they spawn, they dont get the correct skin, one player gets the skin of the other player, and it's weird, how do I fix it? is the getElementModel(source) triggering when another player dies when the other player just died? thats why he's getting the skin of the player that died after him? if so how do I fix it, the code looks fine though.

Link to post
  • Moderators

This "bug" happens because you save it into one variable only. So you always overwrite the previous one.

You should do same as weapons save, something like that:

local playerSkins = {} -- save player's skin in the table, when player dies

addEventHandler ( "onPlayerWasted", root,
	function ( )
	    playerSkins[source] = tonumber(getElementModel(source))
		if ( not playerWeapons [ source ] ) then
			playerWeapons [ source ] = { }
		end
		for slot = 0, 12 do
			local weapon = getPedWeapon ( source, slot )
			if ( weapon > 0 ) then
				local ammo = getPedTotalAmmo ( source, slot )
				if ( ammo > 0 ) then
					if (weapon == 37) then 
						ammo = ammo/10
					end
					playerWeapons [ source ] [ weapon ] = ammo
				end
			end
		end
	end
)

addEventHandler ( "onPlayerSpawn", root,
	function ( )
		if ( playerWeapons [ source ] ) then
		    if getElementData(source, "selecting") == nil then
		        setElementModel(source, playerSkins[source] or 0) 
			for weapon, ammo in pairs ( playerWeapons [ source ] ) do
				giveWeapon ( source, tonumber ( weapon ), tonumber ( ammo ) )
			end
			end
		end
		playerWeapons [ source ] = nil
	end
)
  • Like 1
Link to post
9 hours ago, Patrick said:

This "bug" happens because you save it into one variable only. So you always overwrite the previous one.

You should do same as weapons save, something like that:





local playerSkins = {} -- save player's skin in the table, when player dies

addEventHandler ( "onPlayerWasted", root,
	function ( )
	    playerSkins[source] = tonumber(getElementModel(source))
		if ( not playerWeapons [ source ] ) then
			playerWeapons [ source ] = { }
		end
		for slot = 0, 12 do
			local weapon = getPedWeapon ( source, slot )
			if ( weapon > 0 ) then
				local ammo = getPedTotalAmmo ( source, slot )
				if ( ammo > 0 ) then
					if (weapon == 37) then 
						ammo = ammo/10
					end
					playerWeapons [ source ] [ weapon ] = ammo
				end
			end
		end
	end
)

addEventHandler ( "onPlayerSpawn", root,
	function ( )
		if ( playerWeapons [ source ] ) then
		    if getElementData(source, "selecting") == nil then
		        setElementModel(source, playerSkins[source] or 0) 
			for weapon, ammo in pairs ( playerWeapons [ source ] ) do
				giveWeapon ( source, tonumber ( weapon ), tonumber ( ammo ) )
			end
			end
		end
		playerWeapons [ source ] = nil
	end
)

thanks i got another small issue i wonder if you could help with this, this is C-side and it's got localPlayer, it triggers an event where u click u respawn, but 1 time i died, didnt click respawn button, but probably one other player did, my character got respawned, why tho? 

elseif (source == respawnb ) then
setTimer(function()
setElementData(localPlayer, "dead", false)
end, 2500, 1)
guiSetVisible(respawn, false)
setTimer(function()
triggerServerEvent("respawndeath", localPlayer)
showCursor(false)
end, 2000, 1)

 

the event:

function respawndeath()
setPedStat (client, 24, 570)
local playerDeathTeam = getPlayerTeam ( client) 
if ( playerDeathTeam and getTeamName ( playerDeathTeam ) == "San Andreas Military Forces" ) then 
if getElementData(client, "Class") == "Spec Ops" then
spawnPlayer ( client, 2105.529296875, 892.4794921875, 15.768750190735, 268.6)
return end
setElementData(client, "threat", "low");
spawnPlayer ( client, -1507.4235839844, 2530.9812011719, 56.012500762939)
elseif ( playerDeathTeam and getTeamName ( playerDeathTeam ) == "United Liberation Front" ) then 
setElementData(client, "threat", "low");
spawnPlayer ( client, 128.16796875, -279.15234375, 1.578125)
elseif ( playerDeathTeam and getTeamName ( playerDeathTeam ) == "Outlaws" ) then 
setElementData(client, "threat", "low");
spawnPlayer ( client, -550.44415283203, -2724.2612304688, 138.04649353027)
elseif ( playerDeathTeam and getTeamName ( playerDeathTeam ) == "Civilians" ) then 
setElementData(client, "threat", "low");
spawnPlayer ( client, 2308.8642578125, -9.3994140625, 32.53125)
end
end
addEvent("respawndeath", true)
addEventHandler("respawndeath", root, respawndeath)

i see nothing wrong with it, perhaps the "client" element? do I change it to source instead? or do i define localPlayer as = getLocalPlayer() instead?, localPlayer is pre defined right? so I wont have to do the if localPlayer == getLocalPlayer()?

Edited by Bean666
Link to post

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