Lua 尝试调用字段 'PlayFile'(零值)

Lua attempt to call field 'PlayFile' (a nil value)

我正在尝试为 Garry 的 Mod 创建一个 Lua 插件,但我的代码中一直遇到错误。 这是我的代码:

function say (Player, text, ent)
    s = "/misc/custom/"..text
    s2 = s..".mp3"
    sound.PlayFile(s2)
end
hook.Add("PlayerSay", "Say", say)

这是由此产生的错误。

[saysoundtest25] lua/autorun/chatsounds.lua:4: attempt to call field 'PlayFile' (a nil value)
1. v - lua/autorun/chatsounds.lua:4
2. unknown - lua/includes/modules/hook.lua:84

有什么想法吗?

/lua/autorun文件是服务器端的,有一个变量sound服务器端Lua,但是只有客户端sound.PlayFile存在。

if SERVER then
    AddCSLuaFile() -- We're the server running this code! Let's send it to the client
else -- We're a client!
-- Your code here, only ran by the client!
end

有关详细信息,请参阅 the Garry's Mod wiki 并注意页面上的橙色框,这意味着它是客户端。

请记得检查函数的作用:

sound.PlayFile( string path, string flags, function callback )

示例(取自维基)

sound.PlayFile( "sound/music/vlvx_song22.mp3", "", function( station )
    if ( IsValid( station ) ) then station:Play() end
end )

还有更简单的文档搜索方式:

http://glua.me/

http://glua.me/docs/#?q=sound.PlayFile

DarkRP 如何处理:

https://github.com/FPtje/DarkRP/blob/master/gamemode/modules/chatsounds.lua#L275

Facepunch 上的用户 Robotboy655 帮我解决了这个问题!最终代码:

hook.Add( "PlayerSay", "Say", function( ply, text, team )
BroadcastLua( 'surface.PlaySound("misc/custom/' .. text .. '.mp3")' )
end )

感谢大家的帮助!