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
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.
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.