UI Script is not working?

I am trying to make a ui that shows and dissapears everytime a stat gets added.
the disappearing part is not working, and the ui doesn’t end up in a random location like i intended.

script (main part that isn’t working):

RpS:WaitForChild("GameRemotes"):WaitForChild("Client"):WaitForChild("StatAdd").OnClientEvent:Connect(function(p1, p2)
	local StatAddUI = RpS:WaitForChild("GameUIs"):WaitForChild("StatAdd"):Clone()
	StatAddUI.Parent = plr:WaitForChild("PlayerGui"):WaitForChild("PlayerMain")
	local stat_to_change = StatAddUI:WaitForChild("Stat")
	local amount = StatAddUI:WaitForChild("Amount")
	StatAddUI.Position = UDim2.new(math.random(0.125, 0.875), 0, math.random(0.125, 0.875), 0)
	stat_to_change = p1
	amount = tostring(p2)
	local TweenInf = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut)
	local Goal = {TextTransparency = 1}
	local Tween1 = TS:Create(stat_to_change, TweenInf, Goal)
	local Tween2 = TS:Create(amount, TweenInf, Goal)
	delay(1, function()

i have tried turning the tweens into a while stat_to_change < 1 do statement, also tried asking in a different place.

edit: tween1 gives a “Unable to cast value to Object” error, the “StatAddUI.Position = UDim2.new(math.random(0.125, 0.875), 0, math.random(0.125, 0.875), 0)” part just doesn’t work, no errors


Hey! May I know what exactly is p1 and p2 in your code?

The function math.random(x, y) returns an integer between [x, y], the interval meaning that x and y are included in the range.
To generate random numbers, you should use Random.new():

local seed = Random.new()
local pos = UDim2.fromScale(seed:NextNumber(0.125, 0.875), seed:NextNumber(0.125, 0.875))
-- UDim2.fromScale is just an easier way to edit a UI element's Scale properties

-- rest of your code

If you need more information, you can go here.

1 Like

p1 is the name of the stat that gets changed, p2 is the amount

thanks, this solution solved 1 of the problems.

For your first problem, I think the cause of this error is because you’re tweening a string instead of a UIObject. At this part of your code, you equated stat_to_change and amount to p1 and p2 which consequently make them strings:

stat_to_change = p1
amount = tostring(p2)

Hence, when you apply tween to them, it will output an error. I guess what you’re trying to do here is to change the text value of the Stat UI and the amount UI. Try applying these changes and maybe to could help:

stat_to_change.Text = p1
amount.Text = tostring(p2)

I guess what you’re trying to do here is to change the text value of the Stat UI and the amount UI.

indeed i was. I have no idea how i missed that

1 Like