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

Play Sound In PlayerGui Trough a brick?

Asked by 6 years ago

So! I've been searching for a script 2 days now. But i can't find a solution.

the thing i need: A script that plays a sound localy.

But, whatever i try. I can't do it right.

I've tried:

local player = game.Players:GetPlayerFromCharacter(hit.Parent)

local playerGui = player:WaitForChild("PlayerGui")

playerGui.M3:Play()

(Full script that's required!)


Playing = 0 Gestart = 0 function onTouched(hit) local player = game.Players:GetPlayerFromCharacter(hit.Parent) print(player) wait() if( Gestart == 0) then Gestart = 1 print("Gestart11") wait() if( Playing == 0) then Playing = 1 print("Playing22") (IS SHOULD BE TRIGERED FROM HERE. I've tried this in a LocalScript and in a normal script local playerGui = player:WaitForChild("PlayerGui") wait(script.Parent.WT.Value) playerGui.SoundFolders.SoundFolder1.M3:Play() end end end script.Parent.Parent.Starter.Touched:connect(onTouched)

Thanks for helping guys!

1 answer

Log in to vote
0
Answered by
Rheines 661 Moderation Voter
6 years ago

Your script doesn't work because server scripts cannot access the PlayerGui. To circumvent this, you need to be using remote events.

This script is found inside the brick that is supposed to be touched.

local Players = game:GetService("Players")
--Create a RemoteEvent in Replicated Storage.
local remote = game.ReplicatedStorage.RemoteEvent

local Playing = 0
local Gestart = 0

local function onTouched(hit)
    --Only check for hits from players in the game.
    if hit.Parent:FindFirstChild("Humanoid") then
        --Get the player that hits the part.
        local player = Players:GetPlayerFromCharacter(hit.Parent)
        if Gestart == 0 then Gestart = 1 end
        if Playing == 0 then Playing = 1 end

        --Fire remote event to the player's client.
        remote:FireClient(player)
    end
end

--The brick that is to be touched.
script.Parent.Touched:Connect(onTouched)

When the brick is touched it will fire to the client. There should be a local script to listen to when the part is touched in the PlayerGUI.

local Players = game:GetService("Players")
local RS = game:GetService("ReplicatedStorage")

local player = Players.LocalPlayer

local remote = RS:WaitForChild("RemoteEvent")

--Where the sound is located.
local sound = --location of your sound here relative to your script.

local function PlaySound()
    sound:Play()
end

--Listen for events from the server.
remote.OnClientEvent:Connect(PlaySound)

Also, use :Connect as :connect is deprecated. If it doesn't work please let me know.

0
Thank you! So, i've created the first script as an LocalScript inside the brick. But where do i need to place the second script? Inside the Gui which contains all the sound? or how? HeadlessDeathSpeaker 9 — 6y
0
Oh wait. Nevermind. i've placed it in ReplicatedStorage. But from line 09 i don't know how to acces the player's PlayerGui. I'm fully new to remoteacces and others. HeadlessDeathSpeaker 9 — 6y
0
The first script is supposed to be server scripts or regular scripts, and is inside the brick that you want to touch. The second script put in PlayerGui as a LocalScript. Change line 9 of the LocalScript into the location of the sound. For example, if the LocalScript is located inside the same folder as the sounds, put script.Parent.Sound. I assume your sounds are in a folder inside PlayerGUI. Rheines 661 — 6y
0
Insert also a RemoteEvent into ReplicatedStorage Rheines 661 — 6y
View all comments (4 more)
0
Okido. So i've made a normal script inside the brick with Script 1 in it. The second is placed as an LocalScript inside the Gui. And i've made a RemoteEvent in the ReplicatedStorage. That's correct right? HeadlessDeathSpeaker 9 — 6y
0
So, i've tried what you said. But i get the Error "OnClientEvent is not a valid member of RemoteFunction" from the localscript inside the Gui :O HeadlessDeathSpeaker 9 — 6y
0
(There's also a RemoteEvent in ReplicatedStorage HeadlessDeathSpeaker 9 — 6y
0
Make sure it's a RemoteEvent not a RemoteFunction. I retested everything and it works fine. You got the error because you renamed a RemoteFunction into RemoteEvent, but it is still a RemoteFunction. Rheines 661 — 6y
Ad

Answer this question