Hello, I'm relatively new to scripting and recently worked out how to use remote events and functions. I have created this music script using bindable functions and remote events. The music script works perfectly until interrupted by someone buying a change song developer product which instantly changes the song to the id they put in a textbox. After that happens, the code keeps randomly firing the bindable function and restarting the music script and I have no clue how to fix it. The song is changed when the length of the song is complete for the main music script and in the developer product script, the bindable function to restart the main music script is fired when the length of the song is done.
I am also using 4 different remote events, functions etc. and I am aware that is bad practice so I would appreciate if someone could help me minimize the amount. The bindable function is called 'MusicBought', the remote events are called 'Bought' and 'MusicChanged' and the bindable event is named 'musicChange'.
Main music script (located in ServerScriptService)
local module = {} local MusicPlayer = game:GetService('ServerScriptService'):WaitForChild('Music System') local folder = MusicPlayer.songs local loop = MusicPlayer.settings.loop.Value local waitbetween = MusicPlayer.settings.waitbetweensongs.Value local sound = game:GetService('Workspace'):WaitForChild('Sound') local songs = folder:GetChildren() local ReplicatedStorage = game:GetService("ReplicatedStorage") local MusicChanged = ReplicatedStorage:WaitForChild('MusicChanged') local be = script.Parent:WaitForChild('musicChange') local function musicEvent() while true do local random = (songs)[ math.random( #songs)].value print(random) sound.SoundId = ('rbxassetid://'.. random) local NowPlaying = game:GetService('ReplicatedStorage'):WaitForChild('Playing') NowPlaying.Value = random local Asset = game:GetService("MarketplaceService"):GetProductInfo(random) local name = Asset.Name MusicChanged:FireAllClients(name) print('Attempting to load song "'..name..'"') sound:Play() while wait(.1) do if sound.IsLoaded == true then print('Loaded Audio Time '..sound.SoundId) break end end if sound.Playing == false then print('Loaded Audio Asset '..sound.SoundId) sound:Play() end wait(sound.TimeLength + 2) end end be.Event:connect(musicEvent) return module
Developer product song script (located in ServerScriptService)
-- this is the script for the developer product song local MusicChanged = ReplicatedStorage:WaitForChild('MusicChanged') local MarketplaceService = game:GetService("MarketplaceService") local sound = game:GetService('Workspace'):WaitForChild('Sound') local be = script.Parent:WaitForChild('musicChange') local function processReceipt(receiptInfo) local player = game:GetService("Players"):GetPlayerByUserId(receiptInfo.PlayerId) if not player then return Enum.ProductPurchaseDecision.NotProcessedYet end print(receiptInfo.PlayerId .. " just bought " .. receiptInfo.ProductId) local MusicBought = ReplicatedStorage:WaitForChild('MusicBought') local Bought = ReplicatedStorage:WaitForChild('Bought') Bought:FireClient(player) MusicBought.OnServerInvoke = function(player, Data) local Asset = game:GetService("MarketplaceService"):GetProductInfo(Data) local name = Asset.Name print(Asset.Name) MusicChanged:FireClient(player, name) print('rbxassetid://'.. Data) sound.SoundId = ('rbxassetid://'.. Data) wait() wait(sound.TimeLength) be:Fire() end return Enum.ProductPurchaseDecision.PurchaseGranted end
Initial setup script (located in ServerScriptService)
-- this is the initial setup local be = script.Parent:WaitForChild('musicChange') local songs = { '3340674075', '1346523498', '639750143', '279206904', '2801252370', '2183613911', '1173506587', '3035657191', '1066042031' } local function createSongValue(songID) local IntValue = Instance.new('IntValue') IntValue.Parent = script.Parent.songs IntValue.Value = songID print('Successfully Added Song '.. songID) end local x = 1 while x <= table.getn(songs) do createSongValue(songs[x]) x = x + 1 end print('Song initilazation complete!') require(script.Parent.Main) be:Fire()
The local that sends the song id to the server (located in a Gui)
-- this is the local script in which the song id is sent to the server local Players = game:GetService("Players") local player = Players.LocalPlayer local ReplicatedStorage = game:GetService("ReplicatedStorage") local Bought = ReplicatedStorage:WaitForChild("Bought") local playerGui = player:WaitForChild("PlayerGui") local music = ReplicatedStorage:WaitForChild('Music') local IDEvent = ReplicatedStorage:WaitForChild("MusicBought") local Song = game:GetService('ReplicatedStorage'):WaitForChild('Playing') local NowPlaying = player.PlayerGui:WaitForChild('NowPlaying').Playing local function onNewPlayerFired() local data = playerGui:WaitForChild('SongID').UI.ID.Text IDEvent:InvokeServer(data) print('data sent') end Bought.OnClientEvent:Connect(onNewPlayerFired)
Thanks to anyone that helps
Closed as Too Broad by Ziffixture and Psudar
This question has been closed because it is too broad and is generally unanswerable. Please ask a more specific question.
Why was this question closed?