Lua尝试调用字段'PlayFile'(一个nil值)

我正在尝试为Garry's 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

有什么想法吗?

点赞
用户3340763
用户3340763

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

如果是SERVER则
     AddCSLuaFile() --我们是运行此代码的服务器! 让我们将它发送到客户端
否则 --我们是客户端!
--您的代码,仅由客户端运行!
结尾

有关更多信息,请参见[Garry的Mod Wiki](http://wiki.garrysmod.com/page/sound/PlayFile),注意页面上的橙色框表示它是客户端。

请记住检查该函数需要什么:

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

例如(从wiki中提取)

sound.PlayFile(“sound / music / vlvx_song22.mp3”,“”,functionstationifIsValidstation))then stationPlay()end
end

还有更容易搜索文档的方法:

[http://glua.me/](http://glua.me/)

[http://glua.me/docs/#?q = sound.PlayFile](http://glua.me/docs/#?q = sound.PlayFile)

DarkRP如何处理此问题:

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

2015-01-11 15:10:28
用户4441511
用户4441511

感谢 Facepunch 上的用户 Robotboy655 帮我解决问题!最终的代码:

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

感谢大家的帮助!

2015-01-12 00:12:37