Sound not playing

First LocalScript

local Players = game:GetService("Players")
local plr = Players.LocalPlayer
local gui = plr.PlayerGui:WaitForChild("HUD").BoomboxMenu

repeat wait() until plr.Character

local TextBox = script.Parent.Box
local PlayButton = script.Parent.Play

local RS = game:GetService("ReplicatedStorage")
local EventBoombox = RS:FindFirstChild("BoomboxEvent")

PlayButton.MouseButton1Click:Connect(function()
	if TextBox.Text ~= "" then
		gui:TweenPosition(UDim2.new(0.5,0,1.5,0), 'InOut', 'Sine', 1)
		EventBoombox:FireServer("play", TextBox.Text)
		task.wait(1.5)
		gui.Visible = false
		TextBox.Text = ""
	elseif TextBox.Text == "" then
		gui:TweenPosition(UDim2.new(0.5,0,1.5,0), 'InOut', 'Sine', 1)
		EventBoombox:FireServer("remove", TextBox.Text)
		task.wait(1.5)
		gui.Visible = false
		TextBox.Text = ""
	end
end)

Second LocalScript

local Players = game:GetService("Players")
local plr = Players.LocalPlayer
local MarketplaceService = game:GetService("MarketplaceService")
local RS = game:GetService("ReplicatedStorage")
local DestroyEvent = RS:WaitForChild("DestroyBoombox")
local BoomboxID = 678537021

repeat wait() until plr.Character

local button = script.Parent.Parent
local audioFrame = button.Parent.BoomboxMenu

local Backpack = plr:WaitForChild("Backpack")

plr.CharacterAdded:Connect(function()
	if MarketplaceService:UserOwnsGamePassAsync(plr.UserId, BoomboxID) then
		button.Visible = true
	else
		button.Visible = false
	end
end)

Backpack.ChildAdded:Connect(function()
	local findBoombox = Backpack:FindFirstChild("BoomBox") or Backpack:FindFirstChild("SuperFlyGoldBoombox")
	if findBoombox then
		button.Visible = true
		task.wait(0)
		DestroyEvent:FireServer()
	end
end)

button.ImageButton.MouseButton1Click:Connect(function()
	if audioFrame.Visible then
		audioFrame:TweenPosition(UDim2.new(0.5,0,1.5,0), 'InOut', 'Sine', 1)
		task.wait(1.5)
		audioFrame.Visible = false
	else
		audioFrame.Visible = true
		audioFrame:TweenPosition(UDim2.new(0.5,0,0.5,0), 'InOut', 'Sine', 1)
	end
end)

ServerScript

local RS = game:GetService("ReplicatedStorage")
local BoomboxPart = RS:FindFirstChild("BoomboxPart")
local BoomboxEvent = RS:FindFirstChild("BoomboxEvent")
local DestroyEvent = RS:WaitForChild("DestroyBoombox")

local TS = game:GetService("TweenService")

local info = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, -1, true)

local function playMusic(tween, partClone, soundId)
	tween:Play()
	partClone.Sound.SoundId = "rbxassetid://"..soundId
	partClone.Sound:Play()
	partClone.Sound.Looped = true
end

BoomboxEvent.OnServerEvent:Connect(function(plr, value, soundId)
	if value == "play" then
		if plr.Character:WaitForChild("Torso"):FindFirstChild("BoomboxPart") then
			local part = plr.Character:WaitForChild("Torso"):FindFirstChild("BoomboxPart")
			local tween = TS:Create(part.Mesh, info, {Scale = Vector3.new(4.5,4.5,4.5)})
			playMusic(tween, part, soundId)
		else
			local partClone = BoomboxPart:Clone()
			partClone.Parent = plr.Character:WaitForChild("Torso")
			local weld = partClone:WaitForChild("Weld")
			weld.Part0 = partClone
			weld.Part1 = plr.Character:WaitForChild("Torso")
			local tween = TS:Create(partClone.Mesh, info, {Scale = Vector3.new(4.5,4.5,4.5)})
			playMusic(tween, partClone, soundId)
		end
	elseif value == "remove" then
		local BoomboxPart = plr.Character:WaitForChild("Torso"):FindFirstChild("BoomboxPart")
		BoomboxPart.Sound:Stop()
		BoomboxPart:Destroy()
	end
end)

DestroyEvent.OnServerEvent:Connect(function(plr)
	local findBoombox = plr:WaitForChild("Backpack"):FindFirstChild("BoomBox") or plr:WaitForChild("Backpack"):FindFirstChild("SuperFlyGoldBoombox")
	findBoombox:Destroy()
end)

It is difficult to replicate the exact way your system works on my end.

The error is probably happening here, you should put break points and print statements.

What exactly do you mean by break points?

I think what he means is that the “CharacterAdded” function doesn’t work very well on LocalScripts which is true (atleast from my experience). So what you want to do is make a Variable like for the player owning the gamepass or not so it’s not checking it everytime he respawns. For instance:

local Owned = MarketplaceService:UserOwnsGamePassAsync(plr.UserId, BoomboxID)

if Owned ~= nil then
    button.Visible = true
end

This may work if you put the ResetOnSpawn property as false.

1 Like

Thank you for your reply! Though, I’m not really sure what I’m doing wrong or what I messed up, but it still doesn’t work. It doesn’t even work the first time now. (Well Owned ~= nil does make the button visible and plays the BoomBox, but not for the second time (not fixed yet) and it also appears even when I don’t own the gamepass.)

When I put it to things like Owned == true it did not make it visible (as it shouldn’t, as I don’t own the gamepass), but giving myself the BoomBox wouldn’t make the button visible either, not even for the first time, as it used to before.

If you press F9 on your keyboard when you’re on a certain line of code, something called a breakpoint will appear on the left side of the line. It is used for debugging. When you run your code, the game will pause and pull up that script when whatever code of line the breakpoint is on runs. It basically lets you know if a certain line of code is being reached and run correctly .You can also hold your mouse over variables to see what they are variables for or if their nil.

1 Like

Alright, I set some breakpoints, and I guess I now know where the issue occurs. The whole play function is fine, even the :Destroy() event in the play function (BoomboxPart:Destroy()). However, the RemoteEvent doesn’t work (I believe it doesn’t get fired), because the breakpoint didn’t trigger there.

I’m not really sure what I’m doing wrong though. (It really doesn’t delete the tool)

Backpack.ChildAdded:Connect(function()
	local findBoombox = Backpack:FindFirstChild("BoomBox") or Backpack:FindFirstChild("SuperFlyGoldBoombox")
	if findBoombox then
		button.Visible = true
		task.wait(0)
		DestroyEvent:FireServer(findBoombox)
	end
end)

Wait a minute, this only happens after you reload the character, right?

1 Like

Well I assume so as my loading screen reloads the player.

I believe when receiving an event on the server from the client, you have to make sure you account for the “player” variable on the server side.

DestroyEvent.OnServerEvent:Connect(function(player, findBoombox)
    if findBoombox then
        findBoombox:Destroy()
    end
end)
1 Like

I finally managed to fix this. What I did was that I put the .OnServerEvent call to a plr.CharacterAdded function and put the local Backpack variable inside the function as well.

Though I also did what you just told me about (the player argument in the event), which I assume was also one of the issues.

Thank you!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.