Jump to content

Output something to a team ;)


m4rsje

Recommended Posts

okay, this question is abit harder :P

im trying to make a command voor cops.

when they type the command a marker is being created ,

but the meaning is,

when a player hits the marker(i called the marker m and the createmarker part is already done xD),

the cops have to see their speed.

i tryed getElementVelocity, but i didn't understand it,

so maybe if some1 is realy nice, and gots time,

he can give me an example?:D

thx.

Edited by Guest
Link to comment

Looking at the example on wiki page it has

actualspeed = (speedx^2 + speedy^2 + speedz^2)^(0.5)

So maybe that is the formula for getting the speed, not the velocity. Remember for it to work;

speedx,speedy,speedz = getElementVelocity(...)

Link to comment
  • Discord Moderators
yeah but my problem is ,

how can i make the cops get the speed from the guy that hits the marker ?xD

Well, I assume "cops" is a Team, so you'd want to get the guy's speed when he hits marker, and output it to every player in the team.

local marker = createMarker ( 0, 0, 3, "cylinder", 2 ) -- create the marker, visible to everyone by default
function leakSpeedForCops(hitElement, matchingDimension)
if getElementType (hitElement) == "player" then -- verify the player element
local speedx, speedy, speedz = getElementVelocity (hitElement)
local actualspeed = (speedx^2 + speedy^2 + speedz^2)^(0.5) -- returns one number with decimals, like 0.65.
local cops = getPlayersInTeam ("cops") -- get the players in team "cops", assuming "cops" is a team.
local player = getPlayerName (hitElement) -- get player name
for playerKey, playerValue in ipairs ( cops ) do -- loop through every player in "cops" and output speed
outputChatBox ( player.."'s current speed: "..actualspeed, playerValue) -- output speed
end
end
end
addEventHandler ("onMarkerHit", marker, leakSpeedForCops) -- attach an event handler

Haven't tested it, mistakes reserved.

If cops isn't a team, you can put your players into a table and do the same.

Link to comment

cops is a team, thx ill try it and edit abit, ill add a few things to it.

so only a part of the cops team can see the msg with the position:P

thx ^^

edit: doesn't work, i tryed to fix it but nop :P

errors:

bad argument at 'getPlayersInTeam'
bad argument #1 at ipars 

Link to comment
  • Discord Moderators

Sorry, I don't think getPlayersInTeam wants theTeam argument to be in a string. My bad, replace with this:

local cops = getPlayersInTeam (cops)

And in future, try searching for "getPlayersInTeam" on mtaforums, there are always examples scattered around of proper use.

Link to comment
  • Discord Moderators
That isn't going to work anyway,because it doesn't know what value to choose, and will probably choose itself ending up in an endless loop.

I see....

Fixed the team name and added some other stuff now, working:

local copsTeam = createTeam ( "cops" )
 
local marker = createMarker ( 0, 0, 2, "cylinder", 5 )
function leakSpeedForCops(hitElement, matchingDimension)
if getElementType (hitElement) == "vehicle" then
local speedx, speedy, speedz = getElementVelocity (hitElement)
local actualspeed = (speedx^2 + speedy^2 + speedz^2)^(0.5)
local cops = getPlayersInTeam (copsTeam)
local player = getVehicleOccupant ( hitElement )
local playerName = getPlayerName(player)
for playerKey, playerValue in ipairs ( cops ) do
outputChatBox ( playerName.."'s current speed: "..actualspeed, playerValue)
end
end
end
addEventHandler ("onMarkerHit", marker, leakSpeedForCops)]

I'm sure theres a much more generic way to perform what it does (plus the speed output is kinda useless - might wanna see a speedometer resource for more)

Adding some checks would suit it very nice.

Link to comment

Now i suggest you use this:

local copsTeam =  createTeam (  "cops" )
 
local marker = createMarker ( 0, 0, 2, "cylinder", 5 )
local speedMultiplier = 160
 
function leakSpeedForCops(hitElement, matchingDimension)
if getElementType (hitElement) == "vehicle" then
local velX, velY, velZ = getElementVelocity (hitElement)
local actualspeed = math.sqrt( (velX * velX) + (velY * velY) + (velZ * velZ) ) * speedMultiplier
local cops = getPlayersInTeam (getTeamFromName("cops"))
local player = getVehicleOccupant ( hitElement )
local playerName = getPlayerName(player)
for playerKey, playerValue in ipairs ( cops ) do
outputChatBox ( playerName.."'s current speed: "..actualspeed, playerValue)
end
end
end
addEventHandler ("onMarkerHit", marker, leakSpeedForCops)

That is a bit better, because using that code means you can use it in other files too.

and if you use a good organization of your scripts, you have a file for initialization functions and preparations, and teams are most likely part of that.

I also modified the speed calculation to sqrt, since this is a much better way of calculating.

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