Lua Coding Help

I’m currently running a server and need a script that will provide the model of any prop spawned by a player in the server.

Such as, player spawns a combine helicopter bomb. In console I need it to print to all:

Player(STEAM_0:1:1234567) spawned models/Combine_Helicopter/helicopter_bomb01.mdl

We’ve currently implemented the following code to attempt to print this to console, but it doesn’t seem to work.


function SpawnedProp(ply, model, ent)
	for _,play in pairs(player.GetAll()) do
		if play:IsAdmin() then
			print(ply:Name() .. "(" .. ply:SteamID() .. ") has spawned " .. model )
		end
	end
end 
 
hook.Add("PlayerSpawnedProp", "playerSpawnedProp", SpawnedProp)

Thanks in advance!

Your code refers to ‘ply’ (a nil value). You used ply where you should have used play. Although I recommend changing your play references to ply since ply is a more common name for player variables.

Here’s your code fixed:
[lua]
function SpawnedProp(ply, model, ent)
for _,ply in pairs(player.GetAll()) do
if ply:IsAdmin() then
print(ply:Name() … “(” … ply:SteamID() … ") has spawned " … model )
end
end
end

hook.Add(“PlayerSpawnedProp”, “playerSpawnedProp”, SpawnedProp)[/lua]

Hmm, this still doesn’t seem to want to print to the console. It doesn’t print anything, actually. When I’m trying to get it to print everything any user spawns.

I think this line,


if ply:IsAdmin() then

Could possibly be the issue, is that saying that it only shows admins prop’s being spawned?

Close but not quite.

[lua]
local function SpawnedProp(ply, model, ent)
for _, v in pairs(player.GetAll()) do
if v:IsAdmin() then
v:ChatPrint(ply:Name() … “(” … ply:SteamID() … ") has spawned " … model )
end
end
end

hook.Add(“PlayerSpawnedProp”, “1038471034playerSpawnedProp”, SpawnedProp)
[/lua]

What the OP had was mostly right, but he won’t see it anywhere except either the dedicated server’s console or his own one if it’s a listen server. What I did was changed the print function to player:ChatPrint() so that all admins would see it. I also made the unique string for that hook less likely to cause compatibility problems, and also localised the function because it has quite a generic name.

Looks like it, yes.

I can look to find if there’s a way to send only to admins though.

Edit:

Player:PrintMessage(HUD_PRINTCONSOLE,Message)

So loop through players. If they’re an admin, run this on them. Note only Player and Message require changing. Keep HUD_PRINTCONSOLE.



local function SpawnedProp(ply, model, ent)
    local Players = player.GetAll()
    for _,Ply in pairs(Players) do
        if (Ply:IsAdmin()) then
            Ply:PrintMessage(HUD_PRINTCONSOLE,"Player "..Ply:Nick() .."("..Ply:SteamID()..") has spawned "..model)
        end
    end
end

hook.Add("PlayerSpawnedProp", "playerSpawnedProp", SpawnedProp)


And if you want all admins to see it in their consoles, you’ll need to use v:PrintMessage( 2, etc ) instead of v:ChatPrint( etc )

Thank you for all of your help. And sorry about posting in the wrong section. :smiley: