ROBLOX中本地玩家的声音脚本

我需要一个脚本,每当玩家触摸某个部件时就改变声音。声音应该是特定于玩家的。 我希望脚本在本地玩家上工作,所以无论何时他触摸一个部件,声音应该在他的机器上播放而不是其他玩家。我有一个地方,每个阶段都有独特的声音。就像我的地方有大约15个声音,所以当玩家触摸时它们应该被播放。

stage1->为stage1播放声音 stage2->停止stage1的声音,播放stage2的声音

等等...

点赞
用户3600300
用户3600300

#编写一个脚本,将声音放入 game.Players.LocalPlayer.PlayerGui 中

local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://1234567" -- 填充声音 ID
sound.Parent = game.Players.LocalPlayer.PlayerGui
sound:Play()
2015-03-16 01:02:19
用户4444615
用户4444615

有许多不同的方法可以实现这个功能。其中一种方法是将声音放入 player.Backpack或者player.PlayerGui或者本地游戏的任何位置中的文件夹中,然后在播放之前更改声音ID。

local soundids = {}
soundids[1] = 478920872
soundids[2] = 478920872
soundids[3] = 478920872
soundids[4] = 478920872
soundids[5] = 478920872
soundids[6] = 478920872
soundids[7] = 478920872
soundids[8] = 478920872
soundids[9] = 478920872
soundids[10] = 478920872
soundids[11] = 478920872
soundids[12] = 478920872
soundids[13] = 478920872
soundids[14] = 478920872
soundids[15] = 478920872

local player = game.Players.LocalPlayer  --获取本地玩家

local sound = player.Backpack.Sound    --获取声音NOTE:声音可以保存到其他位置

local leaderstats = player:FindFirstChild("leaderstats")  --获取leaderstats

if leaderstats and leaderstats:FindFirstChild("Level") then   --确保存在leaderstats和leaderstats.level

    local level = leaderstats.Level
    local levelnum = level.Value   --记录level.Value进行比较

    level.Changed:connect(function()  --如果level的属性改变,则运行此函数
        if levelnum ~= level.Value then  --确保更改的属性是Value属性
            levelnum = level.Value      --记录新的Value
            if soundids[levelnum] ~= nil then  --确保soundid存在以避免出错
                sound.SoundId = 'rbxassetid://'..soundids[levelnum]  --将Sound.SoundId更改为对应级别的声音
                sound:Play() --播放声音
            end
        end
    end)
end
2016-03-02 18:08:12