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

Why it loops faster when you touch and end touching the part?

The script works perfectly but when you leave the zone and enter again, the mood.Entertainment.Value increases 2x faster and more than 100, when i leave and enter again, more fast and increases to infinite.

local debounce = false

local touching = {}

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
                touching[player.Name] = true
                while mood.Entertainment.Value < 100 and touching[player.Name] 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
    touching[player.Name] = false
    wait(1)
    debounce = true
end)

4 comments

0

mood.Entertainment.Value = mood.Entertainment.Value + math.random(1,2) means everytime you touch it, it will increase itself by 1 or 2, you can do ood.Entertainment.Value = math.random(1,2) to make it either 1 or 2

0

TouchEnded isn't reliable.

0

Try making a system that uses "Region3". If you need help just ping me in chat

0

or Magnitude

1 answer

✓ Accepted answer

This script looks tasty, but also pretty faulty. I'll give some highlights, and fixes, too!

In cases like this, you're adding either 1 or 2 to the Entertainment value, and it may exceed 100 at some point. Use math.clamp to limit that.

--Line 15
--This will limit your Entertainment value between 0 and 100, preventing it to exceed the limit.
mood.Entertainment.Value = math.clamp(mood.Entertainment.Value + math.random(1,2), 0, 100)

In the TouchEnded event, debounce is set to false upon execution and is set to true right after, while I reckon it's supposed to be the other way around (since this can prevent execution of the Touched event). Perhaps this was a mistake?

--Line 22
--Give this a switcheroo
script.Parent.TouchEnded:Connect(function()
	debounce = true
	--script
	debounce = false
end)

In the same event, player variable does not exist. Hence, touching[player.Name] = false will give out an error. You can do the same player check as the Touched event above. TouchEnded has pretty much the same arguments passing to the connected function, so you can do:

--Line 22
script.Parent.TouchEnded:Connect(function()
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if not player then
		--End the function. The following will not be executed if player does not exist.
		return
	end

	debounce = true
	touching[player.Name] = false
	wait(1)
	debounce = false
end)

That's pretty much all. Aside from those, TTChaos is correct - I recommend you using Region3 if you just need to do this kind of stuf when the player is inside an area or boundaries. Here is a documentation for Region3, and a thorough explanation of Region3 from ClimaxDarkness.


Hmm, was I missing something?

5 comments

0

Works, thank you, but can you help me some with Region3 ?

1

I'm about to sleep so I'm sorry I can't help you much with that, but I can give you some hints so you can do the research on your own. Use Region3.new(minVector3, maxVector3) - minVector3, you can say, is the beginning position of the area, and the other one is the end point. Then, use that variable on workspace:FindPartsInRegion3WithWhiteList().

1

While using the function, remember the second argument of it is a table containing parts that needs to be checked, specifically speaking, a part of the Player. You should make a table containing all HumanoidRootPart references of players - use PlayerAdded and PlayerRemoving events of Players to handle the whitelisted parts.

1

Anyways, once you passed the region3 object and the whitelist to the function, the function will then give you a list of parts within the area AND is a part of the whitelist. By then, you can check if the part is of a player using the method you used in your script - GetPlayerFromCharacter. The rest, it's up to you! Improvise, search for resources on this, etc. to complete this! Goodluck c:

1

https://developer.roblox.com/en-us/api-reference/function/WorldRoot/FindPartsInRegion3 - (there are three functions doing the same thing, but the other two either have blacklist and whitelist, and all has maxParts but only use if you need it since it limits the result)

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