When I reset, this stops working. It's supposed to play a sound on the player's head once you press a button on a GUI. Any way that this can be fixed?

– This creates the sound inside the characters head

local SoundID = “rbxassetid://1838058421” – Put your SoundID here

local PlayerHead = script.Parent.Parent.Character:FindFirstChild(“Head”)

local sound = Instance.new(“Sound”)

sound.Parent = PlayerHead

sound.SoundId = SoundID

– This plays the sound if the player presses the button

local TextButton = script.Parent.Parent.PlayerGui:WaitForChild(“ScreenGui”).TextButton – GUI Button

TextButton.MouseButton1Click:Connect(function()

sound:Play()

end)

^The Script

What you wanna do is create the sound and put it into the head everytime their Character is added. This can be achieved by using Player.CharacterAdded

1 Like

Have you tried putting all inside of the function?
This may be the awnser to your problems.

You cannot detect MouseButton1Click on a GUI from a server-sided script. Instead, you will have to use something called a RemoteEvent in order to fire it from the client to the server. I have included some examples below.

-- Make this a LocalScript inside the TextButton itself.

local TextButton = script.Parent

TextButton.MouseButton1Click:Connect(function()
    game.ReplicatedStorage.RemoteEvent:FireServer() -- again, it may be necessary to change this to reference to your RemoteEvent. Make sure to insert a RemoteEvent. Inserting one into ReplicatedStorage will allow this script to work without changing anything.
end)

Then, from a server-sided script, you can pick up when the event is fired and perform the things on the server.

-- Make this a regular script

-->>: Below is going to insert a sound into the character. 
game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		if not char.Head:FindFirstChild("Sound") then
		        local SoundID = "rbxassetid://1838058421" -- Put your SoundID here
		        local PlayerHead = char:WaitForChild("Head")
		        local sound = Instance.new("Sound")
		        sound.Parent = PlayerHead
			sound.SoundId = SoundID
		end
	end)
end)

-->>: Below will detect when the event is fired and play the sound
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player) -- change this to wherever you put your RemoteEvent. It must match the location of the one you put in the LocalScript. 
    local head = player.Character:FindFirstChild("Head")
	head.Sound:Play()
end)