Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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)


5 comments

0

it works now but it's constantly playing the music

0

This could be because your .Looped variable is set to true. Try setting the sound's .Looped to false.

0

No i mean its constantly instancing the music..

0

Nevermind i used another debounce and it works Thanks

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! :)

This discussion is preserved as it was. New answers, comments, and votes are closed.