Help with script again


#1

What i did wrong with in this script?

local plrpedid = GetPlayerPed(-1)
local alpha = 0

AddEventHandler("playerSpawned", function() 
	GiveWeaponToPed(plrpedid, GetHashKey("weapon_assaultsmg"), 0, false, true)
	SetEntityInvincible(plrpedid, true)
	for i=1,5 do
		SetEntityAlpha(plrpedid, alpha, 1)
		if alpha == 0 then
			alpha = 255
		else
			alpha = 0
		end
		Wait(100)
		i = i + 1
	end
	SetEntityInvincible(plrpedid, false)
	AddAmmoToPed(plrpedid, GetHashKey("weapon_assaultsmg"), 999)
end)

#2

Which part is not working? On a quick glance I don’t see anything wrong and I can understand it that you want to

  1. Give PED a weapon
  2. Make them blink while giving weapon
  3. Give them ammo for weapon at the end

#3

Everything not work for example when i spawn i havent got weapon


#4

I solve this i just eliminate change all “plrpedid” to “GetPlayerPed(-1)” and it works but what mean int skin in SetEntityAlpha?


#5

This script is loading the player ped before the player ped spawns. Move the local plrpedid = GetPlayerPed(-1) to inside of the playerSpawned event handler and it should work.

I would not suggest doing GetPlayerPed(-1) for each of the natives. That makes the game execute the function to get the player ped repeatedly instead of just once. By setting it as a variable at the start, it is more well optimized.

local alpha = 0

AddEventHandler("playerSpawned", function() 
    local plrpedid = GetPlayerPed(-1)
	GiveWeaponToPed(plrpedid, GetHashKey("weapon_assaultsmg"), 0, false, true)
	SetEntityInvincible(plrpedid, true)
	for i=1,5 do
		SetEntityAlpha(plrpedid, alpha, 1)
		if alpha == 0 then
			alpha = 255
		else
			alpha = 0
		end
		Wait(100)
		i = i + 1
	end
	SetEntityInvincible(plrpedid, false)
	AddAmmoToPed(plrpedid, GetHashKey("weapon_assaultsmg"), 999)
end)

#6

Ok, thanks for help and one question. What means “int skin” in native “SetPlayerAlpha” ?


#7

I’m not really familiar with that native, but looking at this thread, it looks like it’s an optional parameter. They have an example function for blinking an entity, you could try theirs and see if/how it works.

Full code snippet with their entity flasher function:

function FlashEntityAlpha(entityToFlash, timesToFlash, flashInterval)
	-- First and foremost, make sure we were given an entity
	if(IsAnEntity(entityToFlash)) then
		-- Make sure we got some valid input, otherwise use defaults
		timesToFlash = (type(timesToFlash) == “number” and timesToFlash or 10)
		flashInterval = (type(flashInterval) == “number” and flashInterval or 150)
		-- Prepare our counter variable
		local flashCount = 0
		-- Let’s use an asynchronous thread
		Citizen.CreateThread(function()
			while (true) do
				Citizen.Wait(flashInterval)
				-- Make sure the entity is still valid before we change the alpha
				if(IsAnEntity(entityToFlash)) then
					-- Check if flashCount is even or an odd number. (if it equals 0, it’s even)
					if(flashCount % 2 == 0) then
						SetEntityAlpha(entityToFlash, 150)
						flashCount = flashCount+1
					else
						SetEntityAlpha(entityToFlash, 255)
						flashCount = flashCount+1
					end
					if(flashCount == timesToFlash) then
						-- When we’ve flashed the specified amount of times, stop the function. Reset alpha just in case.
						SetEntityAlpha(entityToFlash, 255)
						return
					end
				end
			end
		end)
	else
		-- We were not given an entity, so return false
		return false
	end
end

AddEventHandler("playerSpawned", function() 
    local plrpedid = GetPlayerPed(-1)
	GiveWeaponToPed(plrpedid, GetHashKey("weapon_assaultsmg"), 0, false, true)
	SetEntityInvincible(plrpedid, true)
	FlashEntityAlpha(plrpedid, 5, 100)
	SetEntityInvincible(plrpedid, false)
	AddAmmoToPed(plrpedid, GetHashKey("weapon_assaultsmg"), 999)
end)

#8

I did it in this case

local visible = false

AddEventHandler("playerSpawned", function()
	local plrpedid = GetPlayerPed(-1)
	GiveWeaponToPed(plrpedid, GetHashKey("weapon_assaultsmg"), 0, false, true)
	SetEntityInvincible(plrpedid, true)
	for i=0,5 do
		SetEntityVisible(plrpedid, visible)
		if visible then
			visible = false
		else
			visible = true
		end
		Wait(1000)
		i = i + 1
	end
	SetEntityInvincible(plrpedid, false)
	AddAmmoToPed(plrpedid, GetHashKey("weapon_assaultsmg"), 999)
end)

It works so i keep that


#9

Looks good. For legibility, you could remove the if statement to toggle visible. By setting it to not visible, it will switch between true and false. This isn’t much different from an optimization standpoint, but does make it looks a bit cleaner.

local visible = false

AddEventHandler("playerSpawned", function()
	local plrpedid = GetPlayerPed(-1)
	GiveWeaponToPed(plrpedid, GetHashKey("weapon_assaultsmg"), 0, false, true)
	SetEntityInvincible(plrpedid, true)
	for i=0,5 do
        SetEntityVisible(plrpedid, visible)        
        visible = not visible	
		Wait(1000)
		i = i + 1
	end
	SetEntityInvincible(plrpedid, false)
	AddAmmoToPed(plrpedid, GetHashKey("weapon_assaultsmg"), 999)
end)

#10

Thanks for help and tips <3