Lua ROBLOX中本地玩家的声音脚本

Lua ROBLOX中本地玩家的声音脚本,lua,roblox,Lua,Roblox,我需要一个脚本,改变声音为每个球员每当球员触及一个部分。声音应该是特定于播放器的。 我希望脚本在本地播放器上运行,这样每当他触摸到一个部分时,声音都应该在他的机器上播放,而不是在其他播放器上播放。我有一个地方,每个舞台都有独特的声音。像我的地方有大约15种声音,所以他们应该在玩家触摸时播放 stage1->播放stage1的声音 stage2->stage1停止声音,播放stage2声音 因此…制作一个脚本,将声音放入game.Players.LocalPlayer.PlayerGui有许多不同

我需要一个脚本,改变声音为每个球员每当球员触及一个部分。声音应该是特定于播放器的。 我希望脚本在本地播放器上运行,这样每当他触摸到一个部分时,声音都应该在他的机器上播放,而不是在其他播放器上播放。我有一个地方,每个舞台都有独特的声音。像我的地方有大约15种声音,所以他们应该在玩家触摸时播放

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


因此…

制作一个脚本,将声音放入game.Players.LocalPlayer.PlayerGui

有许多不同的方法可以实现这一点。一种方法是将声音放在player.Backpack或player.PlayerGui或localplayer中的任何位置的文件夹中,并在播放之前更改声音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  --get localplayer

    local sound = player.Backpack.Sound    --get sound NOTE:the sound can be                 stored somewhere else

    local leaderstats = player:FindFirstChild("leaderstats")  -- get leaderstats

    if leaderstats and leaderstats:FindFirstChild("Level") then   -- make sure leaderstats and leaderstats.level exists

        local level = leaderstats.Level           
        local levelnum = level.Value   --record level.Value for comparison

        level.Changed:connect(function()  --if a property of level changes then run this function
            if levelnum ~= level.Value then  --make sure that the property changed was the Value property 
                levelnum = level.Value      --record the new Value
                if soundids[levelnum] ~= nil then  --make sure the soundid exists to avoid an error
                    sound.SoundId = 'rbxassetid://'..soundids[levelnum]  --change the Sound.SoundId to the levels' corresponding sound   
                    sound:Play() -- play the sound
                end
            end
        end)
    end 

我试过了,但没有达到预期效果。这是我的脚本。local leaderstats=game.Players.LocalPlayer:FindFirstChild(“leaderstats”)如果leaderstats那么如果leaderstats:FindFirstChild(“Level”),那么在等待时(0)执行本地声音=game.Workspace.SoundModel:GetChildren()对于x=1,如果x==leaderstats.Level.Value则执行声音=声音[x]如果不是leaderstats.Parent.PlayerGui:FindFirstChild(sound),那么对于u,成对的子(game.Players.LocalPlayer.PlayerGui:GetChildren())如果sons.ClassName==“sound”,那么子:Destroy()endend本地克隆=sound:clone()clone.Parent=game.Players.LocalPlayer.PlayerGui克隆:Play()endendendendendendenddendendendend