Jump to content

crear un ped por jugador


MYSOTO

Recommended Posts

Hola a todos bueno estoy creando un sistema de ped pero quiero que solo se pueda crear uno por player ya que abusan del script y ponen muchos y dan lag

aqui les dejo una parte del script:

function loli2( thePlayer ) 
    local x, y, z = getElementPosition( thePlayer ); y = y - 5 
    ped = createPed( 139, x, y, z ) 
    setElementData(ped,"PedExtraHealth",2000)
    setTimer (destroyElement, 500000, 1, ped)
    if ped then 
        follow( ped, thePlayer ) 
    end 
end 
addCommandHandler( "loli", loli2 ) 
Edited by MystoganScripter
Link to comment

From what I have read, you're trying to make it so players can only create one NPC ped so it doesn't cause lag. Easy.

I also made it possible (optional) for them to create their own Ped skin ID, it can be optionally inserted. If "skin" isn't inserted, it'll be '139' (ID) by default. Ped is also created on the creator's rotation. Only 1 ped can be created, made a function where the creator of the ped can delete the ped with /dPed and ped is deleted upon creator's disconnection.

local peds = {}
function pedCreation(thePlayer, command, skin)
if not isElement(peds[thePlayer]) then
if thePlayer and isElement(thePlayer) and getElementType(thePlayer) == "player" then
local x, y, z = getElementPosition(thePlayer)
local _, _, r = getElementRotation(thePlayer)
if x and y and z then
if not skin then skin = 139 end
peds[thePlayer] = createPed(skin, x, y, z, r, false)
if peds[thePlayer] then
outputChatBox("You have successfully created a ped. (ID: ".. skin ..")", thePlayer, 0, 200, 0, false)
else
outputChatBox("Invalid Skin ID.", thePlayer, 200, 0, 0, false)
end
end
end
else
outputChatBox("Use /dPed to remove the existing Ped.", thePlayer, 200, 0, 0, false)
end
end
addCommandHandler("cPed", pedCreation)

function pedDestroy(thePlayer)
if isElement(peds[thePlayer]) then
destroyElement(peds[thePlayer])
peds[thePlayer] = nil
outputChatBox("You have successfully deleted the ped.", thePlayer, 0, 255, 0, false)
else
outputChatBox("Create a ped with /cPed <id>", thePlayer, 0, 200, 0, false)
end
end
addCommandHandler("dPed", pedDestroy)

function pedDestroyQuit()
if isElement(peds[source]) then
destroyElement(peds[source])
peds[source] = nil
end
end
addEventHandler("onPlayerQuit", getRootElement(), pedDestroyQuit)


Si necesitas algo mas, me dejas saber.

  • Like 1
Link to comment
On 28/2/2017 at 21:25, SARSRPG said:

From what I have read, you're trying to make it so players can only create one NPC ped so it doesn't cause lag. Easy.

I also made it possible (optional) for them to create their own Ped skin ID, it can be optionally inserted. If "skin" isn't inserted, it'll be '139' (ID) by default. Ped is also created on the creator's rotation. Only 1 ped can be created, made a function where the creator of the ped can delete the ped with /dPed and ped is deleted upon creator's disconnection.


local peds = {}
function pedCreation(thePlayer, command, skin)
if not isElement(peds[thePlayer]) then
if thePlayer and isElement(thePlayer) and getElementType(thePlayer) == "player" then
local x, y, z = getElementPosition(thePlayer)
local _, _, r = getElementRotation(thePlayer)
if x and y and z then
if not skin then skin = 139 end
peds[thePlayer] = createPed(skin, x, y, z, r, false)
if peds[thePlayer] then
outputChatBox("You have successfully created a ped. (ID: ".. skin ..")", thePlayer, 0, 200, 0, false)
else
outputChatBox("Invalid Skin ID.", thePlayer, 200, 0, 0, false)
end
end
end
else
outputChatBox("Use /dPed to remove the existing Ped.", thePlayer, 200, 0, 0, false)
end
end
addCommandHandler("cPed", pedCreation)

function pedDestroy(thePlayer)
if isElement(peds[thePlayer]) then
destroyElement(peds[thePlayer])
peds[thePlayer] = nil
outputChatBox("You have successfully deleted the ped.", thePlayer, 0, 255, 0, false)
else
outputChatBox("Create a ped with /cPed <id>", thePlayer, 0, 200, 0, false)
end
end
addCommandHandler("dPed", pedDestroy)

function pedDestroyQuit()
if isElement(peds[source]) then
destroyElement(peds[source])
peds[source] = nil
end
end
addEventHandler("onPlayerQuit", getRootElement(), pedDestroyQuit)


Si necesitas algo mas, me dejas saber.

 Disculpa como podria unirlo con este rs para que funcione bien osea que digamos tu ped me siga como en el rs de abajo

function myPed( thePlayer ) 
    local x, y, z = getElementPosition( thePlayer ); y = y - 5 
    ped = createPed( 139, x, y, z ) 
    setElementData(ped,"PedExtraHealth",2000)
    setTimer (destroyElement, 500000, 1, ped)
    if ped then 
        follow( ped, thePlayer ) 
    end 
end 
addCommandHandler( "loli", myPed ) 

function follow(element, player) 
if (not isElement(element) or not isElement(player)) then return end 
if (isPedInVehicle(element)) then return end 
local cX, cY = getElementPosition(player) 
local pX, pY = getElementPosition(element) 
local copangle = (360 - math.deg(math.atan2((cX - pX), (cY - pY)))) % 360 
setPedRotation(element, copangle) 
setElementInterior(element, getElementInterior(player)) 
setElementDimension(element, getElementDimension(player)) 
local dist = getDistanceBetweenPoints2D(cX, cY, pX, pY) 
if (dist > 16) then 
-- Warp 
local x, y, z = getElementPosition(player) 
setElementPosition(element, x, y, z) 
setTimer(follow, 200, 1, element, player) 
elseif (dist > 6) then 
-- Sprint 
setPedAnimation( element, "PED", "sprint_civi" ) 
setTimer(follow, 200, 1, element, player) 
elseif (dist > 4) then 
-- Jog 
setPedAnimation( element, "PED", "JOG_maleA" ) 
setTimer(follow, 200, 1, element, player) 
elseif (dist > 1.5) then 
-- Walk 
setPedAnimation( element, "PED", "WALK_civi" ) 
setTimer(follow, 200, 1, element, player) 
elseif (dist <= 2) then 
-- Stop 
setPedAnimation( element, "PED", "IDLE_stance" ) 
setTimer(follow, 200, 1, element, player) 
end 
end 

Edited by MystoganScripter
Link to comment

Client


local owner = nil
local ped = nil
local animation = false
function followPed()
if (not isElement(owner) or not isElement(ped)) then return end 
if (isPedInVehicle(ped)) then return end
if animation == false then
local cX, cY = getElementPosition(owner) 
local pX, pY = getElementPosition(ped) 
local copangle = (360 - math.deg(math.atan2((cX - pX), (cY - pY)))) % 360 
setPedRotation(ped, copangle) 
setElementInterior(ped, getElementInterior(owner)) 
setElementDimension(ped, getElementDimension(owner)) 
local dist = getDistanceBetweenPoints2D(cX, cY, pX, pY)
if (dist > 16) then
-- Warp 
local x, y, z = getElementPosition(owner) 
setElementPosition(ped, x, y, z) 
animation = true
setTimer(function() animation = false end, 1000, 1)
elseif (dist > 6) then 
-- Sprint 
setPedAnimation(ped, "PED", "sprint_civi")
animation = true
setTimer(function() animation = false end, 1000, 1)
elseif (dist > 4) then 
-- Jog 
setPedAnimation(ped, "PED", "JOG_maleA") 
animation = true
setTimer(function() animation = false end, 1000, 1)
elseif (dist > 1.5) then 
-- Walk 
setPedAnimation(ped, "PED", "WALK_civi") 
animation = true
setTimer(function() animation = false end, 1000, 1)
elseif (dist <= 2) then 
-- Stop 
setPedAnimation(ped, "PED", "IDLE_stance") 
animation = true
setTimer(function() animation = false end, 1000, 1)
end
end
end

function triggerFollow(element, creator)
ped = element
owner = creator
removeEventHandler("onClientRender", getRootElement(), followPed)
addEventHandler("onClientRender", getRootElement(), followPed)
end
addEvent("triggerFollow", true)
addEventHandler("triggerFollow", getRootElement(), triggerFollow)

Server

local peds = {}
function pedCreation(thePlayer, command, skin)
if not isElement(peds[thePlayer]) then
if thePlayer and isElement(thePlayer) and getElementType(thePlayer) == "player" then
local x, y, z = getElementPosition(thePlayer)
local _, _, r = getElementRotation(thePlayer)
if x and y and z then
if not skin then skin = 139 end
peds[thePlayer] = createPed(skin, x, y, z, r, false)
if peds[thePlayer] then
outputChatBox("You have successfully created a ped. (ID: ".. skin ..")", thePlayer, 0, 200, 0, false)
triggerClientEvent(thePlayer, "triggerFollow", getRootElement(), peds[thePlayer], thePlayer)
else
outputChatBox("Invalid Skin ID.", thePlayer, 200, 0, 0, false)
end
end
end
else
outputChatBox("Use /dPed to remove the existing Ped.", thePlayer, 200, 0, 0, false)
end
end
addCommandHandler("cPed", pedCreation)

function pedDestroy(thePlayer)
if isElement(peds[thePlayer]) then
destroyElement(peds[thePlayer])
peds[thePlayer] = nil
outputChatBox("You have successfully deleted the ped.", thePlayer, 0, 255, 0, false)
else
outputChatBox("Create a ped with /cPed <id>", thePlayer, 0, 200, 0, false)
end
end
addCommandHandler("dPed", pedDestroy)

function pedDestroyQuit()
if isElement(peds[source]) then
destroyElement(peds[source])
peds[source] = nil
end
end
addEventHandler("onPlayerQuit", getRootElement(), pedDestroyQuit)


El unico problema es que los demas no pueden ver cuando se mueve. Voy a tratar de hacerte algo mejor. Pero usa este por ahora.

Link to comment
7 hours ago, aka Blue said:

@SARSRPG Eso no tiene sentido, para tener que hacer un sync con todos los jugadores, mejor hacerlo en lado server y ya.

puedes dar un ejemplo? porfa

13 hours ago, SARSRPG said:

Client


local owner = nil
local ped = nil
local animation = false
function followPed()
if (not isElement(owner) or not isElement(ped)) then return end 
if (isPedInVehicle(ped)) then return end
if animation == false then
local cX, cY = getElementPosition(owner) 
local pX, pY = getElementPosition(ped) 
local copangle = (360 - math.deg(math.atan2((cX - pX), (cY - pY)))) % 360 
setPedRotation(ped, copangle) 
setElementInterior(ped, getElementInterior(owner)) 
setElementDimension(ped, getElementDimension(owner)) 
local dist = getDistanceBetweenPoints2D(cX, cY, pX, pY)
if (dist > 16) then
-- Warp 
local x, y, z = getElementPosition(owner) 
setElementPosition(ped, x, y, z) 
animation = true
setTimer(function() animation = false end, 1000, 1)
elseif (dist > 6) then 
-- Sprint 
setPedAnimation(ped, "PED", "sprint_civi")
animation = true
setTimer(function() animation = false end, 1000, 1)
elseif (dist > 4) then 
-- Jog 
setPedAnimation(ped, "PED", "JOG_maleA") 
animation = true
setTimer(function() animation = false end, 1000, 1)
elseif (dist > 1.5) then 
-- Walk 
setPedAnimation(ped, "PED", "WALK_civi") 
animation = true
setTimer(function() animation = false end, 1000, 1)
elseif (dist <= 2) then 
-- Stop 
setPedAnimation(ped, "PED", "IDLE_stance") 
animation = true
setTimer(function() animation = false end, 1000, 1)
end
end
end

function triggerFollow(element, creator)
ped = element
owner = creator
removeEventHandler("onClientRender", getRootElement(), followPed)
addEventHandler("onClientRender", getRootElement(), followPed)
end
addEvent("triggerFollow", true)
addEventHandler("triggerFollow", getRootElement(), triggerFollow)

Server


local peds = {}
function pedCreation(thePlayer, command, skin)
if not isElement(peds[thePlayer]) then
if thePlayer and isElement(thePlayer) and getElementType(thePlayer) == "player" then
local x, y, z = getElementPosition(thePlayer)
local _, _, r = getElementRotation(thePlayer)
if x and y and z then
if not skin then skin = 139 end
peds[thePlayer] = createPed(skin, x, y, z, r, false)
if peds[thePlayer] then
outputChatBox("You have successfully created a ped. (ID: ".. skin ..")", thePlayer, 0, 200, 0, false)
triggerClientEvent(thePlayer, "triggerFollow", getRootElement(), peds[thePlayer], thePlayer)
else
outputChatBox("Invalid Skin ID.", thePlayer, 200, 0, 0, false)
end
end
end
else
outputChatBox("Use /dPed to remove the existing Ped.", thePlayer, 200, 0, 0, false)
end
end
addCommandHandler("cPed", pedCreation)

function pedDestroy(thePlayer)
if isElement(peds[thePlayer]) then
destroyElement(peds[thePlayer])
peds[thePlayer] = nil
outputChatBox("You have successfully deleted the ped.", thePlayer, 0, 255, 0, false)
else
outputChatBox("Create a ped with /cPed <id>", thePlayer, 0, 200, 0, false)
end
end
addCommandHandler("dPed", pedDestroy)

function pedDestroyQuit()
if isElement(peds[source]) then
destroyElement(peds[source])
peds[source] = nil
end
end
addEventHandler("onPlayerQuit", getRootElement(), pedDestroyQuit)


El unico problema es que los demas no pueden ver cuando se mueve. Voy a tratar de hacerte algo mejor. Pero usa este por ahora.

ok gracias

 

Link to comment
  • Recently Browsing   0 members

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