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 7 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??

01cooldown = 50--Put amount of seconds for cooldown to last.
02 
03db = false
04 
05script.Parent.Touched:connect(function(hit)
06if db == false then
07db = true
08local sound50 = Instance.new("Sound", game.Players.LocalPlayer.PlayerGui)
09sound50.Playing = "true"
10sound50.SoundId = "rbxassetid://184219038"
11wait(40)
12sound50:Remove()
13wait(cooldown)
14db = false
15 end
16end)

1 answer

Log in to vote
1
Answered by 7 years ago
Edited 7 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:

01-- SERVER SCRIPT (PUT THIS IN YOUR PART)
02 
03local event = game.ReplicatedStorage.MusicEvent -- This is the remote event.
04 
05script.Parent.Touched:connect(function(hit)
06    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
07    if player ~= nil then
08        event:FireClient(player)
09    end
10end)
11 
12-- LOCAL SCRIPT (PUT THIS IN STARTERGUI)
13 
14local event = game.ReplicatedStorage.MusicEvent -- The remote event.
15 
View all 28 lines...
0
it works now but it's constantly playing the music Cmonlol135 5 — 7y
0
This could be because your .Looped variable is set to true. Try setting the sound's .Looped to false. WelpNathan 307 — 7y
0
No i mean its constantly instancing the music.. Cmonlol135 5 — 7y
0
Nevermind i used another debounce and it works Thanks Cmonlol135 5 — 7y
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 — 7y
Ad

Answer this question