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!
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.