SOLVED: Problem with values

I made a ClickDetector that gives you sanity, but the problem is that it doesn’t work properly. The value number doesn’t just go up by 7; it jumps to a very large number, like 142. In the game, there are 7 of the same object that give you sanity. I’m wondering if this is the issue, because every time I click a different one, the value just keeps increasing to numbers like 142, 170, etc.
image
^ image if your confused of what the item is. another issue is that as soon as i get that huge value it just jumps back to the original value?? im so confused this is probably beginner but i literally have no idea :face_in_clouds: heres the script the one with the for loop is a local script inside of a textlabel

local sanitybar = script.Parent
local rs = game:GetService("ReplicatedStorage")
local val = rs.Values.SanityValue -- number

sanitybar.Text = "Sanity: " .. val.Value -- val

val:GetPropertyChangedSignal("Value"):Connect(function()
	sanitybar.Text = "Sanity: " .. val.Value -- update text
end)

for i = val.Value, 0, -1 do
	task.wait(1) -- wait time
	val.Value = i -- Update value
end
local sanitygainer = script.Parent
local rs = game:GetService("ReplicatedStorage")
local val = rs.Values.SanityValue -- number
local ss = game:GetService("SoundService")
local misc = ss.Misc

sanitygainer.MouseClick:Connect(function()
	val.Value += 7
	misc.Vitamin:Play()
	sanitygainer.Parent:Destroy()
end)

^server script. Sorry for yapping but it would be appreciated if someone helped cuz im so confused rn. (beginner scripter btw :sob: )

what are you trying to do with this?
i tested this script in roblox studio and the problem is:
i̶t̶ j̶u̶s̶t̶ r̶e̶s̶e̶t̶s̶ b̶a̶c̶k̶ t̶o̶ z̶e̶r̶o̶ n̶o̶ m̶a̶t̶t̶e̶r̶ t̶h̶e̶ v̶a̶l̶u̶e̶ (NOT THE PROBLEM)

1 Like

huh ? well the value is automatically set to 100 cuz i set it to that in rs

Alr i’ll try to replicate what you did and try to fix it.

Are you trying to make the ‘sanity’ not go over ‘100’ when you click the ‘sanitygainer’?
if that’s it then i found the solution!
use this code:

val.Value += 7
print(val.Value .. "before")
--if val is over 100 then set it back to 100
if val.Value >= 100 then val.Value = 100 end
print(val.Value .. "after")

and replace it with this in your script:

val.Value += 7

While taking a look at this problem again, i could have found another one.
this:

sanitygainer.MouseClick:Connect(function()

you said its a server script right? and its also parented to the part that was clicked,
the problem could be to why the value rises so high out of nowhere can happen because it was triggered multiple times at once causing the value added to increase more than expected.

consider adding a cooldown variable wich prevents the code inside the click function from runing again while it still is runing

local cooldown = false
sanitygainer.MouseClick:Connect(function()
--if the cooldown bool is set to true then return. (wont run the code below)
if cooldown then return end
cooldown = true -- set the cooldown bool to true after it runs

-- put any other code that you want here:




wait(1)--this is the cooldown timer, without this, the code would run instantly making the bool useless

cooldown = false --set the bool back to false wich lets the code able to run again
end)

or you can just turn the canquery collision property inside the sanity object off which makes the mouse ignore the object

if you have any other problems with this let me know.

yep ill try this when im free, and let u know if it works. ty in advance!

It works but like as soon as it goes to 100 it instantly goes back to its original value, so i had 70 sanity and i drank it, it went to 100 then instantly went back to 70 or lower i forgot

and now the sanity is kinda broken cause i just ate a drink and it just made my sanity 100 and its not going down or anything

nevermind, ive fixed it by putting the for loop into a server script for the sanity to go down. Did not know it was that simple…

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