[英]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.