Jump to content

Sync peds?


Bean666

Recommended Posts

Hi, this script works fine if you're seeing it on your own perspective, you could see the zombies walking smoothly and fine, but when you see others being chased by their own zombies, the zombies is kinda laggy and kinda slow, is there anyway to sync with other players on what you're seeing as well? this is a clientside script so. yeah that might be the problem but im sure there might be something?  the peds are created serverside, but the chase function is on client.

Chase function:

local function trackMe()
	
	local zombies = getElementsByType("ped",resourceRoot,true)
	
	for index,zombie in ipairs(zombies) do
		if math.random(1,60) == 15 and isPedDead(zombie) == false then
			local sound = playSound3D("sounds/mgroan"..tostring(math.random(1,10))..".ogg",zombie.position)
			setSoundMaxDistance(sound, 30)
		end
		local zombieTarget = zombieData[zombie].target
		if zombieTarget and isElement(zombieTarget) then
			local lx,ly,lz = getElementPosition(zombieTarget)
			local lVector = Vector3(lx,ly,lz)
			local hVector = Vector3(getPedBonePosition(zombie,6))
			local zVector = Vector3(getElementPosition(zombie))
			local doesZombieSeePlayer = isLineOfSightClear(hVector,lVector,true,false,false,true,false,true,true,zombie)
			local distanceToPlayer = getDistanceBetweenPoints2D(zVector.x,zVector.y,lVector.x,lVector.y)
			if timesExecuted%5 == 0 and zombieData[zombie].target == localPlayer then
				local newTarget,doesSee,distance = getNewTarget(zombie)
				if newTarget and doesSee and distance < distanceToPlayer and newTarget ~= zombieData[zombie].target then
					triggerServerEvent("Zday:setZombieNewTarget",zombie,newTarget)
				end
			end
			if zombieData[zombie].target == localPlayer and doesZombieSeePlayer == false and isPedDead(zombieTarget)==false then
				local newTarget,doesSee = getNewTarget(zombie,localPlayer)
				if newTarget and doesSee and newTarget ~= zombieData[zombie].target then
					triggerServerEvent("Zday:setZombieNewTarget",zombie,newTarget)
				end
			end
			if zombieData[zombie].hunting then
				if not zombieData[zombie].positions then zombieData[zombie].positions = 0 end
				if not zombieData[zombie].paths then zombieData[zombie].paths = {} end
				local dist = false
				if #zombieData[zombie].paths > 1 then
					local llx,lly,llz = unpack(zombieData[zombie].paths[#zombieData[zombie].paths-1])
					dist = getDistanceBetweenPoints3D(llx,lly,llz,lx,ly,lz)
				end
				if dist and dist > 0.7 then
					table.insert(zombieData[zombie].paths,{lx,ly,lz,getPedControlState(zombieTarget,"jump"),getPedControlState(zombieTarget,"sprint")})
				elseif not dist then
					table.insert(zombieData[zombie].paths,{lx,ly,lz,getPedControlState(zombieTarget,"jump"),getPedControlState(zombieTarget,"sprint")})
				end
				if zombieData[zombie].paths[zombieData[zombie].positions+1] and not zombieData[zombie].eating then
					local nx,ny,nz,jump,sprint = unpack(zombieData[zombie].paths[zombieData[zombie].positions+1])
					local nVector = Vector3(nx,ny,nz)
					local isClear = isLineOfSightClear(zVector,nVector,true,true,false,true,false,true,true,zombie)
					local notMoving = false
					if zombieData[zombie].lastPosition then
						local dist = getDistanceBetweenPoints3D(zombieData[zombie].lastPosition,zVector)
						if dist < 0.01 then
							local action = math.random(1,2)
							if action == 1 then
								setPedControlState(zombie,"fire",true)
							elseif action == 2 then
								notMoving = true
							end
						else
							setPedControlState(zombie,"fire",false)
						end
					end
					local needToJump = (jump or getPedSimplestTask(zombie) == "TASK_SIMPLE_CLIMB" or arePedLegsBlocked(zombie) or notMoving)
					if needToJump then
						if (getPedMoveState(zombie) ~= "jump" or getPedMoveState(zombie) ~= "climb") then setPedControlState(zombie,"forwards",false) end
						if sprint then setPedControlState(zombie,"sprint",true) end
						setPedControlState(zombie,"jump",true)
					else
						setPedControlState(zombie,"sprint",false)
						setPedControlState(zombie,"jump",false)
					end
					local dist = getDistanceBetweenPoints2D(zVector.x,zVector.y,nVector.x,nVector.y)
					local pathDistance = getZombieCalculatedPathDistance(zombie)
					local diff = pathDistance/distanceToPlayer
					if pathDistance > distanceToPlayer and diff > 1.1 and doesZombieSeePlayer then
						zombieData[zombie].paths = {}
						zombieData[zombie].positions = 0
						zombieData[zombie].changingPath = true
						zombieData[zombie].changePathTick = getTickCount()
					end
					if zombieData[zombie].changingPath then
						if (getTickCount() - zombieData[zombie].changePathTick) > 500 then
							zombieData[zombie].changingPath = nil
							zombieData[zombie].changePathTick = nil
						end
					end
					local angle = rot(zVector.x,zVector.y,nVector.x,nVector.y)
					setPedCameraRotation(zombie,-angle)
					setPedControlState(zombie,"forwards",true)
					if zombie.inWater then
						setElementRotation(zombie,0,0,angle)
						setPedControlState(zombie,"sprint",true)
					elseif not sprint then
						setPedControlState(zombie,"sprint",false)
					end
					zombieData[zombie].lastPosition = Vector3(getElementPosition(zombie))
					if isClear and dist < 1 then
						zombieData[zombie].positions = zombieData[zombie].positions + 1
					end
				else
					if isPedDead(zombie) == false and getDistanceBetweenPoints3D(zVector,lVector) < 1 and not playersDoomed[zombieTarget] then
						murderPlayerByZombie(zombieTarget,zombie)
						if isPedDead(zombieTarget) then 
							playersDoomed[zombieTarget] = true
						end 
					elseif getDistanceBetweenPoints3D(zVector,lVector) < 1 and ((isPedDead(zombieTarget) or zombieTarget.health<1) and playersEatable[zombieTarget] and not playerEated[zombieTarget]) then
						playerEated[zombieTarget] = true
						zombieData[zombie].eating = true
						setPedAnimation(zombie,"MEDIC","cpr",-1,false,true,false)
						setTimer(resetZombieAnimation,10000,1,zombie)
					end
					setPedControlState(zombie,"forwards",false)
				end
			else
				if doesZombieSeePlayer and not zombieData[zombie].hunting then
					zombieData[zombie].hunting = true
					zombieData[zombie].positions = 0
				elseif not zombieData[zombie].hunting then
					local newTarget = getNewTarget(zombie,localPlayer),doesSee
					if newTarget and doesSee and not zombieData[zombie].changingTarget then
						triggerServerEvent("Zday:setZombieNewTarget",zombie,newTarget)
						zombieData[zombie].changingTarget = true
					end
				end
				if not zombieData[zombie].changingPath then
					setPedControlState(zombie,"forwards",false)
				end
			end
		else
			setPedControlState(zombie,"forwards",false)
		end
	end
	
	timesExecuted=timesExecuted+1

end

setTimer(trackMe,100,0)	

 

Edited by Bean666
Link to post
1 hour ago, Bean666 said:

yeah that might be the problem but im sure there might be something?

You can reduce the ped sync interval from 400 to 200 (mtaserver.conf):

https://wiki.multitheftauto.com/wiki/Server_mtaserver.conf#ped_sync_interval

(https://wiki.multitheftauto.com/wiki/SetServerConfigSetting)

The server has to be stopped first in case of editing the config file.

Link to post
13 minutes ago, IIYAMA said:

You can reduce the ped sync interval from 400 to 200 (mtaserver.conf):

https://wiki.multitheftauto.com/wiki/Server_mtaserver.conf#ped_sync_interval

(https://wiki.multitheftauto.com/wiki/SetServerConfigSetting)

The server has to be stopped first in case of editing the config file.

will 100 hurt the server?

as when i start the script an info is saying it needs to set ped sync interval to a 100.

Edited by Bean666
Link to post
17 minutes ago, Bean666 said:

will 100 hurt the server?

as when i start the script an info is saying it needs to set ped sync interval to a 100.

According to wiki the minimum for peds value is 200. If you can set it to 100, it shouldn't be working, that is if wiki is up to date.

Will it hurt the server? That depends on the internet connection of the players. 😭

Link to post
1 minute ago, IIYAMA said:

According to wiki the minimum for peds value is 200. If you can set it to 100, it shouldn't be working, that is if wiki is up to date.

RQZxc2E.png

it says valid 50-4000 so yeah maybe 100 is valid though idk ill try.

  • Like 1
Link to post

tried 100 ped sync interval, works better than before but other's peds still kinda looking stuttery. 

local function trackMe()
	
	local zombies = getElementsByType("ped",resourceRoot,true)
	
	for index,zombie in ipairs(zombies) do
		if math.random(1,60) == 15 and isPedDead(zombie) == false then
			local sound = playSound3D("sounds/mgroan"..tostring(math.random(1,10))..".ogg",zombie.position)
			setSoundMaxDistance(sound, 50)
		end
		local zombieTarget = zombieData[zombie].target
		if zombieTarget and isElement(zombieTarget) and isPedDead(zombieTarget) == false then
			local lx,ly,lz = getElementPosition(zombieTarget)
			local lVector = Vector3(lx,ly,lz)
			local hVector = Vector3(getPedBonePosition(zombie,6))
			local zVector = Vector3(getElementPosition(zombie))
			local doesZombieSeePlayer = isLineOfSightClear(hVector,lVector,true,false,false,true,false,true,true,zombie)
			local distanceToPlayer = getDistanceBetweenPoints2D(zVector.x,zVector.y,lVector.x,lVector.y)
			if timesExecuted%5 == 0 and zombieData[zombie].target == localPlayer then
				local newTarget,doesSee,distance = getNewTarget(zombie)
				if newTarget and doesSee and distance < distanceToPlayer and newTarget ~= zombieData[zombie].target then
					triggerServerEvent("Zday:setZombieNewTarget",zombie,newTarget)
				end
			end
			if getDistanceBetweenPoints3D(zVector,lVector) > 100 then 
				triggerServerEvent("Zday:destroyZombie",zombie)
			end 
			if zombieData[zombie].hunting then
				if not zombieData[zombie].positions then zombieData[zombie].positions = 0 end
				if not zombieData[zombie].paths then zombieData[zombie].paths = {} end
				local dist = false
				if #zombieData[zombie].paths > 1 then
					local llx,lly,llz = unpack(zombieData[zombie].paths[#zombieData[zombie].paths-1])
					dist = getDistanceBetweenPoints3D(llx,lly,llz,lx,ly,lz)
				end
				if dist and dist > 0.7 then
					table.insert(zombieData[zombie].paths,{lx,ly,lz,getPedControlState(zombieTarget,"jump"),getPedControlState(zombieTarget,"sprint")})
				elseif not dist then
					table.insert(zombieData[zombie].paths,{lx,ly,lz,getPedControlState(zombieTarget,"jump"),getPedControlState(zombieTarget,"sprint")})
				end
				if zombieData[zombie].paths[zombieData[zombie].positions+1] and not zombieData[zombie].eating then
					local nx,ny,nz,jump,sprint = unpack(zombieData[zombie].paths[zombieData[zombie].positions+1])
					local nVector = Vector3(nx,ny,nz)
					local isClear = isLineOfSightClear(zVector,nVector,true,true,false,true,false,true,true,zombie)
					local notMoving = false
					if zombieData[zombie].lastPosition then
						local dist = getDistanceBetweenPoints3D(zombieData[zombie].lastPosition,zVector)
						if dist < 0.01 then
							local action = math.random(1,2)
							if action == 1 then
								setPedControlState(zombie,"fire",true)
							elseif action == 2 then
								notMoving = true
							end
						else
							setPedControlState(zombie,"fire",false)
						end
					end
					local needToJump = (jump or getPedSimplestTask(zombie) == "TASK_SIMPLE_CLIMB" or arePedLegsBlocked(zombie) or notMoving)
					if needToJump then
						if (getPedMoveState(zombie) ~= "jump" or getPedMoveState(zombie) ~= "climb") then setPedControlState(zombie,"forwards",false) end
						if sprint then setPedControlState(zombie,"sprint",true) end
						setPedControlState(zombie,"jump",true)
					else
						setPedControlState(zombie,"sprint",false)
						setPedControlState(zombie,"jump",false)
					end
					local dist = getDistanceBetweenPoints2D(zVector.x,zVector.y,nVector.x,nVector.y)
					local pathDistance = getZombieCalculatedPathDistance(zombie)
					local diff = pathDistance/distanceToPlayer
					if pathDistance > distanceToPlayer and diff > 1.1 and doesZombieSeePlayer then
						zombieData[zombie].paths = {}
						zombieData[zombie].positions = 0
						zombieData[zombie].changingPath = true
						zombieData[zombie].changePathTick = getTickCount()
					end
					if zombieData[zombie].changingPath then
						if (getTickCount() - zombieData[zombie].changePathTick) > 500 then
							zombieData[zombie].changingPath = nil
							zombieData[zombie].changePathTick = nil
						end
					end
					local angle = rot(zVector.x,zVector.y,nVector.x,nVector.y)
					local mutatedZombie = getElementData(zombie, "mutatedZombie")
					setPedCameraRotation(zombie,-angle)
					setPedControlState(zombie,"forwards",true)
					if mutatedZombie == "Runner" then 
						setPedControlState(zombie,"sprint",true)
					end 
					if zombie.inWater then
						setElementRotation(zombie,0,0,angle)
						setPedControlState(zombie,"sprint",true)
					elseif not sprint then
						if mutatedZombie == "Runner" then 
							setPedControlState(zombie,"sprint",true)
						else
							setPedControlState(zombie,"sprint",false)
						end 
					end
					zombieData[zombie].lastPosition = Vector3(getElementPosition(zombie))
					if isClear and dist < 1 then
						zombieData[zombie].positions = zombieData[zombie].positions + 1
					end
					if getDistanceBetweenPoints3D(zVector,lVector) < 4 then 
						if getPedControlState(zombie, "sprint") == true then 
							setPedControlState(zombie, "sprint", false)
						end 
					end 
				else
					if getDistanceBetweenPoints3D(zVector,lVector) > 100 then 
						triggerServerEvent("Zday:destroyZombie",zombie)
					elseif isPedDead(zombie) == false and getDistanceBetweenPoints3D(zVector,lVector) < 2 and not playersDoomed[zombieTarget] then
						murderPlayerByZombie(zombieTarget,zombie)
						if isPedDead(zombieTarget) then 
							playersDoomed[zombieTarget] = true
						end
					elseif getDistanceBetweenPoints3D(zVector,lVector) < 1 and ((isPedDead(zombieTarget) or zombieTarget.health<1) and playersEatable[zombieTarget] and not playerEated[zombieTarget]) then
						playerEated[zombieTarget] = true
						zombieData[zombie].eating = true
						setPedAnimation(zombie,"MEDIC","cpr",-1,false,true,false)
						setTimer(resetZombieAnimation,10000,1,zombie)
					end
					if getDistanceBetweenPoints3D(zVector,lVector) < 4 then 
						if getPedControlState(zombie, "sprint") == true then 
							setPedControlState(zombie, "sprint", false)
						end 
					end 
					setPedControlState(zombie,"forwards",false)
				end
			else
				if doesZombieSeePlayer and not zombieData[zombie].hunting then
					zombieData[zombie].hunting = true
					zombieData[zombie].positions = 0
				elseif not zombieData[zombie].hunting then
					local newTarget = getNewTarget(zombie,localPlayer),doesSee
					if newTarget and doesSee and not zombieData[zombie].changingTarget then
						triggerServerEvent("Zday:setZombieNewTarget",zombie,newTarget)
						zombieData[zombie].changingTarget = true
					end
				end
				if not zombieData[zombie].changingPath then
					setPedControlState(zombie,"forwards",false)
				end
			end
		else
			setPedControlState(zombie,"forwards",false)
			triggerServerEvent("Zday:delayDestroyZombie", zombie)
		end
	end
	
	timesExecuted=timesExecuted+1

end

setTimer(trackMe,100,0)	

client follow func.

 

syncer(Server):

local function setZombieTarget(zombie,target)

	if target and isElement(target) then
		triggerClientEvent(root,"Zday:setZombieTarget",zombie,zombie,target)
		zombieTargets[zombie] = target
		if getElementSyncer(zombie) ~= target then
			setElementSyncer(zombie,target)
		end
	end

end

 

overall nothings wrong, it's just the sync.

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