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

How would I make a brick play music in a players PlayerGui when touched? [closed]

Asked by 5 years ago

Im not sure what to do for this. Im trying to get a brick, when touched by a player, to play or stop an audio thats located in the PlayerGui. How would that be done? Im just bad with referencing the players of the game and all that hullabaloo.

Closed as Not Constructive by RubenKan

This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.

Why was this question closed?

2 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Do not make requests on Scripting Helpers

You may post a question with an attempt attached, but you may not make requests. I'm feeling nice though, and yeah im trying to beat incapaz in this rep race so ill explain piece by piece lol.

Server-Side

This issue will require two scripts: a server and a local script. The server script will handle the actual Touched event, and the local script will handle the sound. The server script should be placed in ServerScriptService, and the local script should be placed somewhere like StarterPlayerScripts. This code will cause the brick to act like a toggle button. That is, if the sound is off, it will turn it on and vice versa. To make the sound play, we have to use a remote event to communicate with the client, because server scripts cannot access existing members of PlayerGui.

--Server Script

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remote = Instance.new("RemoteEvent") --create remote
Remote.Parent = ReplicatedStorage
local part = workspace:WaitForChild("Part") --your part in workspace

part.Touched:Connect(function(hit)
   local player = Players:GetPlayerFromCharacter(hit.Parent)
   if player then --check existance
      Remote:FireClient(player,"") --you can send any data through the second argument
   end
end)

Client-Side

Here, we'll simply handle the sound. Once the remote event is fired, we'll check to see if the sound already exists in the client's PlayerGui. If it does, we'll just either play it or stop it, depending on its current state. Otherwise, we'll create a new sound and do the same process.

--Local Script

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remote = ReplicatedStorage:WaitForChild("RemoteEvent")
---
local MusicId = 000 --change this to your SoundId

Remote.OnClientEvent:Connect(function(args) --the data you sent
   local music = player.PlayerGui:FindFirstChild("Music")
   if not music then --if it doesnt already exist
      music = Instance.new("Sound")
      music.Name = "Music"
      music.SoundId = MusicId
      music.Parent = player.PlayerGui
   end
   if music.IsPlaying == true then
      music:Stop()
   else
      music.TimePosition = 0
      music:Play()
   end
end)

Please read through this and leave any questions you may have in the comments. Remember not to request anything here in the future.

Resources:

:GetPlayerFromCharacter()

Remote Events

Touched

Sounds

Accept and upvote if this helps!

0
when incapaz is in the lead this happens eyyy theking48989987 2147 — 5y
0
shut up king no 1 asked ur opinion!! just upvote me :)) Gey4Jesus69 2705 — 5y
0
thats not a convincing argument for upvoting :3 theking48989987 2147 — 5y
Ad
Log in to vote
0
Answered by
dionant 23
5 years ago
Edited 5 years ago

In a nutshell:

Part.Touched passes an objectvalue which is the touched part. That part's parent is the player's character. Ensure the part's parent EDIT: has an humanoid. If so, Part.Parent.Name is the player's username.

Then you go to game.Players and do :FindFirstChild(Part.Parent.Name)

This website is not a place where people will code for you. Put effort into finding a way to do what you want and then come ask questions here only if after trying to actually do something.

Here: http://wiki.roblox.com/index.php?title=Touched