Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Music isn't playing when i touch a part??

Asked by 6 years ago

Basically when i step on a part a sound will play inside the players PlayerGui and this works PERFECTLY in studio but when i go to the actual game when i step on the part it does not instance the sound in the PlayerGui help??

cooldown = 50--Put amount of seconds for cooldown to last.

db = false

script.Parent.Touched:connect(function(hit)
if db == false then 
db = true
local sound50 = Instance.new("Sound", game.Players.LocalPlayer.PlayerGui)
sound50.Playing = "true"
sound50.SoundId = "rbxassetid://184219038"
wait(40)
sound50:Remove()
wait(cooldown)
db = false 
 end 
end)

1 answer

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

You can't call LocalPlayer from a server script. If you wanted to achieve this, you could send a RemoteEvent to the player. The RemoteEvent would then allow the sound to be played in the local script.

It works in studio because there is only one local player (hence game.Players.LocalPlayer) being an instance.

The following script will work for your instance with a little revamp:

-- SERVER SCRIPT (PUT THIS IN YOUR PART)

local event = game.ReplicatedStorage.MusicEvent -- This is the remote event.

script.Parent.Touched:connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player ~= nil then
        event:FireClient(player)
    end
end)

-- LOCAL SCRIPT (PUT THIS IN STARTERGUI)

local event = game.ReplicatedStorage.MusicEvent -- The remote event.

local debounce = false
local cooldown = 50

game.ReplicatedStorage.MusicEvent.OnClientEvent:connect(function(info)
    if debounce then return end -- If the debounce is true then stop the script
    local sound50 = Instance.new("Sound", game.Players.LocalPlayer.PlayerGui)
    sound50.SoundId = "rbxassetid://184219038"
    sound50:Play() -- More efficient way to play.
    wait(40)
    sound50:Destroy() -- More updated version of removing an object
    wait(cooldown)
    debounce = false
end)


0
it works now but it's constantly playing the music Cmonlol135 5 — 6y
0
This could be because your .Looped variable is set to true. Try setting the sound's .Looped to false. WelpNathan 307 — 6y
0
No i mean its constantly instancing the music.. Cmonlol135 5 — 6y
0
Nevermind i used another debounce and it works Thanks Cmonlol135 5 — 6y
0
Oh, I see what you mean. It was my fault, I forgot to add a debounce = true part in the script, my apologies! Anyway, glad it works for you! :) WelpNathan 307 — 6y
Ad

Answer this question