0

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

有任何想法吗?

4

2 回答 2

1

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

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

感谢大家的帮助!

于 2015-01-12T00:12:37.360 回答
0

/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'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

于 2015-01-11T15:10:28.267 回答