Generating random values doesn't work

Hello forum, today i have a script that randomize the value at a fast speed (most of the time i put it at 0.05 but sometime i put it at 0.1). For some reason, the script didn’t work. I did some fixing and it worked a little bit. This happens every time i try to do it and today i wanted to seek for helps on what could possibly go wrong with it. Here’s the script:

first variant:

local value = -- value here

while task.Wait() do
   value.Value = math.random(-- value1, -- value2)
end

second variant:

local value = -- value here

while task.Wait() do
   local randomval = math.random(-- 1, -- 2)
   value.Value = randomval
end
1 Like

Everything here seems to be fine except for one thing. Consider replacing task.Wait() with task.wait(). Not sure if this is auto-correct kicking you or not, but it won’t work if any word in wait() is a capital.

2 Likes

i put it inside a textlabel (which is inside a ui), can that be the problem?

1 Like

The script, alongside the value should update properly even if it’s inside a GUI or textlabel. If you’re updating the textlabel, ensure that you replace .Value with .Text.

I’ve done the same method as you and everything seems to be working fine.
TextLabel changes:
RobloxStudioBeta_uoIF1Eyi9i
Value changes:
RobloxStudioBeta_KIvwLlFIUx

1 Like

Hello. Can you please provide screenshots about where are the script, value and textlabel? Also can you give us the full script?

1 Like

here
Screenshot 2024-06-27 160607
its inside the image label, the value is below the script i want to use it for, and the randomizing script is below the value.
The script i used is the one i provided when creating this topic.

1 Like

can you please rename scripts and provide full code of first and second? (also tell where is each)

1 Like

wait. Maybe you use math.random(0.05, 0.1)? If yes, then instead use (math.random(5, 10) * 0.01)

Just a thing for ya but math.random does not accept decimal values, only integers. You would have to divide the random value later on to get integers.

The first one is basically just a status effect script that i used for my game, the second one is the main randomize script. Both of them are in the imagelabel, which is in a frame and inside a gui.
The first

local player = game:GetService("Players")
local char = player.LocalPlayer.Character
local hum = char:WaitForChild("Humanoid")
local oldhealth = hum.Health
local TweenService = game:GetService("TweenService")
local frame = script.Parent


-- Function to move the frame
local function moveFrame(destinationPosition, duration)
	frame.Visible = true
	local tweenInfo = TweenInfo.new(
		duration, -- Duration in seconds
		Enum.EasingStyle.Sine, -- Easing style
		Enum.EasingDirection.Out, -- Easing direction
		0,
		false,
		0 
	)

	local goal = {Position = destinationPosition}

	local tween = TweenService:Create(frame, tweenInfo, goal)

	tween:Play()

	tween.Completed:Wait()
end

local function returnFrame(originalPosition, duration)
	local tweenInfo = TweenInfo.new(
		duration, -- Duration in seconds
		Enum.EasingStyle.Sine, -- Easing style
		Enum.EasingDirection.Out, -- Easing direction
		0,
		false,
		0 
	)

	local goal = {Position = originalPosition}

	local tween = TweenService:Create(frame, tweenInfo, goal)

	tween:Play()

	frame.Visible = false
end

local function bleed()
		for i = 0, 50, 3 do
			wait(0.5)
			hum.Health -= 0.075
		end
		script.Parent.Visible = false
end

hum:GetPropertyChangedSignal("Health"):Connect(function()
	if hum.Health < oldhealth then
		oldhealth = hum.Health
			moveFrame(UDim2.new(-0.016, 0, 0.011, 0), 1)
			bleed() 
			returnFrame(UDim2.new(0, 0, 0.868, 0), 1)  -- Return frame to original position (change UDim2.new(0, 0, 0, 0) to the actual original position)
	end
end)

hum.Died:Connect(function()
	if frame.Visible == true then
		frame.Visible = false
	end
end)

the second one:

local value = script.Parent

while task.wait() do
	value.Value = math.random(1, 3)
end

Screenshot 2024-06-27 165829

You’ve been on the devforum quite a lot today… I’ve seen you everywhere! Anyways, the OP did not recall any errors, just the value not changing, which I believe I now know the problem…

@BaconVXD Are you perhaps using a NumberValue for this? math.random() returns the randomized numbers as a string. Due to this, math.random() needs to be wrapped in a tonumber() for it to work with NumberValues. Here’s a script for that:

local value = script.Parent

while task.wait() do
	value.Value = tonumber(math.random(1, 3)) -- Convert to a "number" (which only NumberValue supports)
end

…no…? how did you even come to that conclusion? That has never been true in any version of Roblox.
image

I suspect OP is in fact using math.random() wrong and just using decimals (which are only supported by the Random APIs NextNumber functionality.)

1 Like

Nevermind to my first reply in the case of using math.random() with decimals, but are you ever using the Value of this Value? And is this GUI under StarterGui? (LocalScripts won’t run outside of StarterGui or within StarterPlayerScripts/StarterCharacterScripts)

Well damn, I’ve been proven wrong. It still does not work with NumberValues without tonumber though. I used no decimals, yet I ran into the exact same issue OP had, and that was the fact that the value wasn’t being changed. I switched it to a StringValue without any changes and it went through just fine. No errors were put in the output. Is this perhaps a bug?


Again, no…?
image

Edit: And also, using while task.wait() do is considered bad practice as it requires waiting for the task.wait() to return and then having to ensure it is a truthy value. Wait inside your loops not inline with their statements.

Ohh, no I was very wrong here. Bahaha, my bad!

@BaconVXD You’re updating the value too often that it just appears as the same number. Explorer tends to not update that fast. Thanks to Bothered for correct this mistake of mine :pray:

I did it both on a TextLabel and a NumberValue and they both definitely updated. NumberValue looks to be exactly 0, whilst TextLabel does indeed change on the screen. I did updates on both and sure enough, they are printing the same thing.

1 Like

Yes i did use the value of it. And… yes of course i put the gui in starter gui (testing…)

I don’t see where you’re using the Value though… And I’d probably give yourself a larger span of randoms than 3 since that’s going to give you a 33% chance for all numbers.

Sorry here, my bad for not adding the wait() in all the script i provided. Yes i did add a wait() under the while do function. However (i just tested this recently), i turn it into wait(1) and it gives the same 0 values

What does that have to do woth the solution? I am not everywhere on the forum atleast. Just here to help the people… and well seems like you did not at all find the solution here…

To OP @BaconVXD

Could there be a script changing the value again? Or no? Or something?