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