Announce ITEM:OnBuy(ply)

I’m trying to have it so when a player buys a specific weapon in the point shop, it announces a message in the middle of everyone’s screen. Is this possible with a point shop item? I was gonna use HUD_PRINTTALK, but that will just announce it in the chat box right?

Can I use ulx somehow to do csay when a player buys a item using this?



function ITEM:OnBuy(ply)
ply:Give(self.WeaponClass)
end


[LUA]
for _, p in pairs( player.GetAll() ) do
p:PrintMessage( HUD_PRINTCENTER, ply:Nick() … " bought " … self.Name )
end
[/LUA]

should be fine

Thanks so much!

If I also wanted to add a sound that played, would this work?


ply:SendLua('surface.PlaySound("sound/item1.wav")

so all together


function ITEM:OnBuy(ply)
ply:Give(self.WeaponClass)
for _, p in pairs( player.GetAll() ) do
p:PrintMessage( HUD_PRINTCENTER, ply:Nick() .. " bought " .. self.Name )
ply:SendLua('surface.PlaySound("sound/item1.wav")
end

? Would adding that sound work? Would it play for everyone or just the person?

Since you put it in the loop change this:

[lua]
ply:SendLua('surface.PlaySound(“sound/item1.wav”)
[/lua]
to
[lua]
p:SendLua(‘surface.PlaySound(“sound/item1.wav”’)
[/lua]

It would only play for the player as you´re not using the second arg in the for loop to send lua with and you miss [[ CODE ]] in SendLua

it would play for the person for each person on the server, and spam them.

you could just use
[LUA]BroadcastLua( “surface.PlaySound(‘item1.wav’)” )[/LUA]

so

[LUA]
function ITEM:OnBuy(ply)
ply:Give(self.WeaponClass)
for _, p in pairs( player.GetAll() ) do
p:PrintMessage( HUD_PRINTCENTER, ply:Nick() … " bought " … self.Name )
end
BroadcastLua( “surface.PlaySound(‘item1.wav’)” )
end
[/LUA]

That worked, thank you. Any way to make the text stay longer? Or change the color/font?

Stay longer. You could set a variable when the player buys and set a timer to set the boolean to false after the amount of time. timer.Simple(time, function() code end)

Change font and color you need to do ^^ too. draw.Text

you can’t change it without writing your own function and networking that afaik

Got everything working perfect. Thanks for the help guys.