Is there a better way to implement this? Basically, what this remote function does is, when a player clicks a button, it generates a random number, plays a sound and displays that number in a GUI above their head.
This code here works perfectly:
local connection = sendServerDiceRollEvent.OnServerEvent:Connect(function(player) -- Server receives this function whenever a player clicks a button
if not table.find(randomDicePlayersClicked, player) then -- if the randomDicePlayersClicked table did not find the player
table.insert(randomDicePlayersClicked, player); -- insert their instance into the table
randomDiceGuiClone = randomDiceGui:Clone();
number = returnRandomNumber(1, 6);
randomDiceGuiClone.text.Text = number;
randomDiceGuiClone.Parent = game.Workspace:WaitForChild(player.Name).Head;
randomDiceGuiClone.Enabled = true;
-- Print the dice roll number above the player's head
if game.Players:FindFirstChild(player.Name) then
-- Find that player's seat and play a dice roll sound from it
soundClone = diceRollSounds[returnRandomNumber(1, #diceRollSounds)]:Clone(); -- Make a clone of a random dice roll sound
soundClone.Parent = player.Character.Humanoid.SeatPart.Parent;-- Add sound to player's seat
soundClone.Volume = 5;
soundClone:Play();
soundClone.Ended:Wait();
-- Delete the sound after playing
soundClone:Destroy();
However, if you set the text and enable the GUI after soundCllone.Ended:Wait(); then numbers start popping up weird and sometimes the GUI won’t even display:
local connection = sendServerDiceRollEvent.OnServerEvent:Connect(function(player) -- Server receives this function whenever a player clicks a button
if not table.find(randomDicePlayersClicked, player) then -- if the randomDicePlayersClicked table did not find the player
table.insert(randomDicePlayersClicked, player); -- insert their instance into the table
randomDiceGuiClone = randomDiceGui:Clone();
number = returnRandomNumber(1, 6);
-- Print the dice roll number above the player's head
if game.Players:FindFirstChild(player.Name) then
-- Find that player's seat and play a dice roll sound from it
soundClone = diceRollSounds[returnRandomNumber(1, #diceRollSounds)]:Clone(); -- Make a clone of a random dice roll sound
soundClone.Parent = player.Character.Humanoid.SeatPart.Parent;-- Add sound to player's seat
soundClone.Volume = 5;
soundClone:Play();
soundClone.Ended:Wait();
-- Delete the sound after playing
soundClone:Destroy();
--- HERE IS WHERE THE PROBLEM HAPPENS
randomDiceGuiClone.text.Text = number;
randomDiceGuiClone.Parent = game.Workspace:WaitForChild(player.Name).Head;
randomDiceGuiClone.Enabled = true;