Leaderstats number value subtracts not specified numbers?

Hello, i wanna substract an leaderstats value, the text button has an attribute called isWrong. When isWrong is true and we clicked the button then it subtracts the number: math.random(1, 2.5) (Its a variable called num and it returns the number to the variable) so it subtracts num (for ex. 2.3)
If u dont know what im talking about see my last post.
When its subtracting my value went from 103 to 74??
Anyway heres the LOCAL script:

	if v:GetAttribute("isWrong") == true then
			v.Text = "❌ - "..num
			game.Players.LocalPlayer.leaderstats["Trading Bucks"].Value = game.Players.LocalPlayer.leaderstats["Trading Bucks"].Value - num
		end	

Sorry if im unclear, here is a video.

robloxapp-20220829-1529460.wmv (1.4 MB)

math.random(1, 2.5)

This is only going to return 1 or 2 due to the nature in which math.random works.

math.random(10, 25) / 10

Yep, i know, it said 1 but its subtracting 37. It should subtract 1.

Probably a client-server replication issue, make sure all value changes are only occuring on the server.

Its a local script, watch the video if you are confused.

make sure all value changes are only occuring on the server.

You should also provide the entire script.

local num = math.random(1, 2.5) -- creates a number for each right tile

local function revealTiles()
	for i, v in pairs(script.Parent.Parent:GetChildren()) do
		if v:GetAttribute("isWrong") == true then
			v.Text = "❌ - "..num
			game.Players.LocalPlayer.leaderstats["Trading Bucks"].Value = game.Players.LocalPlayer.leaderstats["Trading Bucks"].Value - num -- // subtracting 37 or random number here that is not specified
		else
			v.Text = "πŸ’Ž - "..num
			v:SetAttribute("canBeSelected", false)
		end
	end
end

script.Parent.MouseButton1Click:Connect(function()
	if script.Parent:GetAttribute("isWrong") == true then
		script.Parent.Text = "❌"
		for i, v in pairs(script.Parent.Parent:GetChildren()) do
			v.Active = false
			wait(.01)
			revealTiles()
		end
	elseif script.Parent:GetAttribute("canBeSelected") == false then
		script.Parent.Active = false
		error("You cannot click this item!")
	elseif script.Parent:GetAttribute("isWrong") == false and script.Parent:GetAttribute("canBeSelected") == true then
		script.Parent.Text = "πŸ’Ž - "..num
		game.Players.LocalPlayer.leaderstats["Trading Bucks"].Value = game.Players.LocalPlayer.leaderstats["Trading Bucks"].Value + num
	end
end)

Its a local script, how should i do it to an server script, that would like destroy everything

local function revealTiles()
	for i, v in pairs(script.Parent.Parent:GetChildren()) do
		if v:GetAttribute("isWrong") == true then
			v.Text = "❌ - "..num
			game.Players.LocalPlayer.leaderstats["Trading Bucks"].Value = game.Players.LocalPlayer.leaderstats["Trading Bucks"].Value - num -- // subtracting 37 or random number here that is not specified
		else
			v.Text = "πŸ’Ž - "..num
			v:SetAttribute("canBeSelected", false)
		end
	end
end

You’re subtracting 1 or 2 for each of the children, use a β€˜break’ statement to break the loop where necessary.

Im confused, uh did you change something there i dont see any changes

Ohhhhhhh

limitlimitlimitlimitlimit

now its subtracting 12?? it doenst work

Where did you put the break statement?

use a β€˜break’ statement to break the loop where necessary.

You should know where the loop needs to be broken.

nevermind i fixed it myself
thank you still

local num = math.random(1, 2.5) -- creates a number for each right tile

local function revealTiles()
		if script.Parent:GetAttribute("isWrong") == true then
			script.Parent.Text = "❌ - "..num
		else
			script.Parent.Text = "πŸ’Ž - "..num
			script.Parent:SetAttribute("canBeSelected", false)
	end
	end
script.Parent.MouseButton1Click:Connect(function()
	if script.Parent:GetAttribute("isWrong") == true then
		script.Parent.Text = "❌"
		game.Players.LocalPlayer.leaderstats["Trading Bucks"].Value = game.Players.LocalPlayer.leaderstats["Trading Bucks"].Value - num
			script.Parent.Active = false
			wait(.01)
			revealTiles()
	elseif script.Parent:GetAttribute("canBeSelected") == false then
		script.Parent.Active = false
		error("You cannot click this item!")
	elseif script.Parent:GetAttribute("isWrong") == false and script.Parent:GetAttribute("canBeSelected") == true then
		script.Parent.Text = "πŸ’Ž - "..num
		script.Parent.Active = false
		game.Players.LocalPlayer.leaderstats["Trading Bucks"].Value = game.Players.LocalPlayer.leaderstats["Trading Bucks"].Value + num
	end
end)
elseif script.Parent:GetAttribute("canBeSelected") == false then
script.Parent.Active = false
error("You cannot click this item!")

You should use warn here instead, error will terminate the stack. As suspected the issue was with your loop, a correctly placed break statement would have fixed it too.

2 Likes

Thank you for your information, adding additional details is just very nice of you! Thank you! Really!