So I'm tryna make a jojo game and finally for the love of god made quotes working, so now I made multiple and put them all in a data table and used the math.random function to randomly make them play each time the remote event is fired. However, sometimes the remote event fires but the quote and sound doesn't play. It's weird and I don't know why it's happening. Here's the scripts.
Client script =
local player = game.Players.LocalPlayer -- our player local userinputservice = game:GetService("UserInputService") local enabled = false -- begone spam local Quote = script.Parent:WaitForChild("Quote") userinputservice.InputBegan:Connect(function(Input, GPE) if not GPE then if Input.KeyCode == Enum.KeyCode.N and enabled == false then enabled = true Quote:FireServer() wait(7) enabled = false end end end)
Server script =
local remote = script.Parent:WaitForChild("Quote") remote.OnServerEvent:Connect(function(player) print("large joe") if math.random(1,3) == 1 then local Billboard = script:WaitForChild("BillboardGui"):Clone() local sound = Billboard.Sound Billboard.Parent = player.Character.Head sound:Play() wait(6.5) Billboard:Destroy() elseif math.random(1,3) == 2 then print("joe larg") local Billboard2 = script:WaitForChild("BillboardGui2"):Clone() local Sound = Billboard2.Sound Billboard2.Parent = player.Character.Head Sound:Play() wait(6.5) Billboard2:Destroy() elseif math.random(1,3) == 3 then local Billboard3 = script:WaitForChild("BillboardGui3"):Clone() local Sound = Billboard3.Sound Billboard3.Parent = player.Character.Head Sound:Play() wait(5.2) Sound:Stop() wait(1.5) Billboard3:Destroy() end end)
(btw these were put in StarterPack)
Is there anyway I can fix this?
Try storing your math.random in a variable. The reason is:
1) If you are doing math.random function as a condition for each if statement, there's a possible chance for it to not generates the required number causing nothing to happen.
If you don't get what I am trying to say:
-- example if math.random(1, 3) == 1 then -- only runs when it generates 1 print("yay1") elseif math.random(2, 3) == 1 then -- only runs when it generates 2 print("yay2") end -- if none of the reqs are met then nothing will happen
Instead of doing math.random in each if statement you want to do:
local chosenNumber = math.random(1, 3) -- 1 to 3 if chosenNumber = 1 then -- bla bla bla, your code here elseif chosenNumber = 2 then -- bla bla bla, your code here elseif chosenNumber = 3 then -- bla bla bla, your code here end
Also, there's a chance for it to generates the same number in a row, because it's only 33% ( 1 out of 3 ) chance for each number to be generated.
I tried putting the math.random variable inside of the onserverevent and now it works.
I tried putting the math.random variable inside of the onserverevent and now it works.