[英]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
有关更多信息,请参见Garry的Mod Wiki ,并注意页面上的橙色框,这表示它是客户端。
请记住还要检查该功能需要执行的操作:
sound.PlayFile( string path, string flags, function callback )
示例(摘自Wiki)
sound.PlayFile( "sound/music/vlvx_song22.mp3", "", function( station )
if ( IsValid( station ) ) then station:Play() end
end )
搜索文档的更简单方法:
http://glua.me/docs/#?q=sound.PlayFile
DarkRP如何处理此问题:
https://github.com/FPtje/DarkRP/blob/master/gamemode/modules/chatsounds.lua#L275
Facepunch上的Robotbot655用户帮助我解决了这个问题! 最终代码:
hook.Add( "PlayerSay", "Say", function( ply, text, team )
BroadcastLua( 'surface.PlaySound("misc/custom/' .. text .. '.mp3")' )
end )
谢谢大家的帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.