Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why my value increases to unlimited?

When i enter the Entertainment giver zone, it starts giving me Entertainment every 0.4s, but it won't end at mood.Entertainment.Value being 100, it still goes until infinite, and when i leave the Entertainment giver zone and go in again, it gives me 10x Entertainment faster. Also i want it to be like when you stop touching, the value stops increasing, when you touch it again the value increases again.

local debounce = false

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if script.Parent.Parent.Status.Value == true then
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			local mood = game.ReplicatedStorage[player.Name.."'s Mood"]
			if mood.Entertainment.Value < 100 and debounce == false then
				debounce = true
				while true do
					wait(0.4)
					mood.Entertainment.Value = mood.Entertainment.Value + math.random(1,2)
				end
			end
		end
	end
end)

script.Parent.TouchEnded:Connect(function()
	debounce = false
	wait(1)
    debounce = true
end)

2 answers

You need to make the loop only run while mood.Entertainment.Value is less than 100:

local debounce = false

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        if script.Parent.Parent.Status.Value == true then
            local player = game.Players:GetPlayerFromCharacter(hit.Parent)
            local mood = game.ReplicatedStorage[player.Name.."'s Mood"]
            if mood.Entertainment.Value < 100 and debounce == false then
                debounce = true
                while mood.Entertainment.Value < 100 do
                    wait(0.4)
                    mood.Entertainment.Value = mood.Entertainment.Value + math.random(1,2)
                end
            end
        end
    end
end)

script.Parent.TouchEnded:Connect(function()
    debounce = false
    wait(1)
    debounce = true
end)

2 comments

0

Also when touch ends, and i touch the part again it goes faster and the value goes more than 100

0

I want it to be like when you stop touching, the value stops increasing, when you start touching it again, it starts increasing again.

Your while true do loop on line 10 will never end.

Make your if statement on line 8 the condition of the loop

if not debounce then
	debounce = true
	while mood.Entertainment.Value < 100 do
		wait(0.4)
		mood.Entertainment.Value += math.random(1, 2)
	end
	debounce = false
end

2 comments

0

why not using the condition of the loop? its there for that

0

I did not think about that at first glance. I have since improved upon my answer.

This discussion is preserved as it was. New answers, comments, and votes are closed.