Ulysses
General => Developers Corner => Topic started by: Zmaster 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
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
-
OnRoundSet is a serverside hook (https://github.com/Mr-Gash/GMod-Deathrun/blob/master/deathrun/readme.txt#L67). 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 (https://pastebin.com/JpdVbVNJ) if you aren't able to do it yourself (I couldn't hide it in a spoiler or anything).
-
Okay I'll try to figure it out using the info you gave me, thanks!
-
Alright, here's the code I ended up using (thanks to Decicus)
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?
(http://puu.sh/awaUy/ed3a8ba747.jpg)
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
-
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:
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++.
-
Completed code with the chat print added. Use team.GetName to get the name of the team from the index.
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
-
You were missing a ) on line 11, Cobalt, but thanks to both of you :P