I need help again for make this script only run once instead of running infinitely this script uses math.random

why hello there, I I have little bit of problems going on with my game’s random welcome screen random text. it was supposed to decide on letters and stop running but instead the script runs more then once and it forms weird hunk of random words, kinda like this. (I suck at scripting so bad)

I have tried to look for other topics related to math.random script but I couldn’t find any that could be supported with my script, which is shown in below.

local tip1 = script.Parent.Text1 --these local tips are welcome screen random tips and there is 15 of them, and it was supposed to show one text per one session, but the script keep repeating the thing,
local tip2 = script.Parent.Text2
local tip3 = script.Parent.Text3
local tip4 = script.Parent.Text4
local tip5 = script.Parent.Text5
local tip6 = script.Parent.Text6
local tip7 = script.Parent.Text7
local tip8 = script.Parent.Text8
local tip9 = script.Parent.Text9
local tip10 = script.Parent.Text10
local tip11 = script.Parent.Text11
local tip12 = script.Parent.Text12
local tip13 = script.Parent.Text13
local tip14 = script.Parent.Text14
local tip15 = script.Parent.Text15


while true do
	local randomtip = math.random(1,15) --the script I'm wishing to fix it
	wait(0.1)
	if randomtip == 1 then	
		tip1.Visible = true
	end
	if randomtip == 2 then	
		tip2.Visible = true
	end
	if randomtip == 3 then	
		tip3.Visible = true
	end
	if randomtip == 4 then	
		tip4.Visible = true
	end
	if randomtip == 5 then	
		tip5.Visible = true
	end
	if randomtip == 6 then	
		tip6.Visible = true
	end
	if randomtip == 7 then	
		tip7.Visible = true
	end
	if randomtip == 8 then	
		tip8.Visible = true
	end
	if randomtip == 9 then	
		tip9.Visible = true
	end
	if randomtip == 10 then	
		tip10.Visible = true
	end
	if randomtip == 11 then	
		tip11.Visible = true
	end
	if randomtip == 12 then	
		tip12.Visible = true
	end
	if randomtip == 13 then	
		tip13.Visible = true
	end
	if randomtip == 14 then	
		tip14.Visible = true
	end
	if randomtip == 15 then	
		tip15.Visible = true
	end
end

and thank you for reading all of these again, I couldn’t script alone so your help is gladly needed, thank you. -mari

this is painful. Learn how to use dictonaries and tables please. and also loops

this is by far the worst code I have seen in a while.

1st create a table of all the tips
2nd call a random value using math.random()

local tips = {}
local tip = tips[math.random(1,#tips)]

For example if you wanted to add the text

“text inside of the parenthesis”

as a random tip, you could do

local tips = {
"text inside of the parenthesis"
}

if you wanted to add a second tip you just add a comma to show you’re going to the next tip.
for example:

local tips = {
"tip #1",
"tip #2",
"tip #3"
}

it will now generate a random tip from that list when you call

local tip = tips[math.random(1,#tips)]
3 Likes

It’s a loop, so it will run more than once.

Same, this is seriously painful to look.

Just use this simplified version of it:

local tipTable = {}

for index, tip in ipairs(script.Parent:GetChildren()) do
	if tip:IsA("TextBox") then
		tipTable[index] = tip
	end
end

while someCondition do -- fix this to your own liking
	local randomTipNumber = math.random(15)
	tipTable[randomTipNumber].Visible = true
end

NOTE: This is just a reference, so change anything that is necessary.

1 Like

It’s better practice to not use textboxes and instead use the tips inside the dictonary as raw string. Its simply a superior method in every conceivable way.

My method should be used not this one. My method is simply significantly better practice. It allows them to add tips very quickly and also since it goes off the length of the table they don’t have to manually manage the math.random(1,15)

1 Like

Can you show your code? I don’t quite get what you just said.

please go to my previous post.

hey thank you for helping me with that script, everything is working handy, sorry for showing you literal yandere dev scale of spaghetti code in this topic through, I hope you that you will have amazing days, tysm! :hugs:

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