ULX

Author Topic: Hook giving winning team points  (Read 3289 times)

0 Members and 4 Guests are viewing this topic.

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
Hook giving winning team points
« on: July 29, 2014, 09:17:56 AM »
I found a Deathrun gamemode on Facepunch and I want to make a script that gives every player on the winning team of a round a certain amount of points for PointShop

I have the hook ready, I'm just not sure how to give the points to everyone on the team

Here's the function for giving points ply:PS_GivePoints(10)

And here's what I have so far
Code: [Select]
if SERVER then
AddCSLuaFile() -- Send the file to the client.

hook.Add( "OnRoundSet", "DR Money", function( round, winner )

if round == ROUND_ENDING then

end

end )
end

I would imagine that winner:PS_GivePoints(10) wouldn't work since winner would be a team and not the players on the team

Offline Decicus

  • Hero Member
  • *****
  • Posts: 552
  • Karma: 81
    • Alex Thomassen
Re: Hook giving winning team points
« Reply #1 on: July 29, 2014, 10:18:41 AM »
OnRoundSet is a serverside hook. No need to do "AddCSLuaFile".

You could loop through players table using a "for" loop (with "player.GetAll()"), and then check if ply:Team() is the same as the "winner"-team.
Then after that, you can give the points using Player:PS_GivePoints(10).

I have the code ready which should work (I haven't played or done any deathrun stuff, so I'm not 100% sure), but if you'd like to give it a go yourself then you should try to use some of my tips (hope I didn't forget anything).

The code can be found here if you aren't able to do it yourself (I couldn't hide it in a spoiler or anything).
Contact information:
E-mail: alex@thomassen.xyz.
You can also send a PM.

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
Re: Hook giving winning team points
« Reply #2 on: July 29, 2014, 10:25:45 AM »
Okay I'll try to figure it out using the info you gave me, thanks!

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
Re: Hook giving winning team points
« Reply #3 on: July 29, 2014, 11:02:07 AM »
Alright, here's the code I ended up using (thanks to Decicus)
Code: [Select]
if SERVER then
        hook.Add( "OnRoundSet", "DR Money", function( round, winner )
                if round == ROUND_ENDING then
                        for _, ply in ipairs( player.GetAll() ) do
                                if ply:Team() == winner then   
                                        ply:PS_GivePoints( 25 )
                                end
if winner == TEAM_DEATH then
for k, ply in pairs( player.GetAll() ) do
ply:ChatPrint( "The Death team has been given 25 points for winning!" )
end
else
for k, ply in pairs( player.GetAll() ) do
ply:ChatPrint( "The Runner team has been given 25 points for winning!" )
end
end
                        end
                end
        end )
end

And it works fine
However, when the round ends and it puts one of those messages in chat, it'll usually put it in twice (and sometimes three times) instead of just once
How would I make it put in that message only once?


Edit: The code shown in this post isn't making "The 2 team" anymore, that screenshot was made before I changed it to say Runner or Death
« Last Edit: July 29, 2014, 11:08:22 AM by Zmaster »

Offline Decicus

  • Hero Member
  • *****
  • Posts: 552
  • Karma: 81
    • Alex Thomassen
Re: Hook giving winning team points
« Reply #4 on: July 29, 2014, 11:22:19 AM »
You included your if/else statement inside the first for loop. Thus it printed to everyone, then looped again to print another time.
It's a bit difficult to explain, so I'll just do this:
Code: [Select]
if SERVER then
        hook.Add( "OnRoundSet", "DR Money", function( round, winner )
                if round == ROUND_ENDING then
                        for _, ply in ipairs( player.GetAll() ) do
                                if ply:Team() == winner then   
                                        ply:PS_GivePoints( 25 )
                                end
                                -- Your print code is here.
                        end
                        -- It should be here
                end
        end )
end

Also, I don't know if it was just the code tags, but you should probably work on your tabs/spaces for each line. The chatprinting code was a bit hard to read for me without putting it into Notepad++.
Contact information:
E-mail: alex@thomassen.xyz.
You can also send a PM.

Offline Cobalt

  • Full Member
  • ***
  • Posts: 216
  • Karma: 44
  • http://steamcommunity.com/id/__yvl/
Re: Hook giving winning team points
« Reply #5 on: July 29, 2014, 11:36:27 AM »
Completed code with the chat print added. Use team.GetName to get the name of the team from the index.
Code: [Select]
if SERVER then
hook.Add( "OnRoundSet", "DR Money", function( round, winner )
if round == ROUND_ENDING then
for k, v in next, player.GetAll() do
if not v:Team() == winner then
continue
end
v:PS_GivePoints( 25 )
end
local name = team.GetName( winner )
PrintMessage( HUD_PRINTTALK, "Players on the " .. name .. " team were given 25 points for winning."
end
end )
end

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
Re: Hook giving winning team points
« Reply #6 on: July 29, 2014, 11:46:10 AM »
You were missing a ) on line 11, Cobalt, but thanks to both of you :P