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

What is Wrong with this script?

local v = game.Players.LocalPlayer
if v.Name == "Player1" or game:GetService("MarketplaceService"):PlayerOwnsAsset(v,440723046)==true then
	if v:FindFirstChild('leaderstats') then
			if v.leaderstats:FindFirstChild('XP') then
				v.leaderstats.XP.Changed:connect(function(z)
					v.leaderstats.XP.Value = v.leaderstats.XP.Value + 25 * workspace.Scripts.Multiplier.Value
				end)
			end
	end
end

Because when i test i get 19:40:47.528 - Maximum event re-entrancy depth exceeded for IntValue.Changed 19:40:47.529 - While entering function defined in script 'Players.Player1.PlayerGui.DoubleXP', line 5

3 comments

0

Can anyone solve this?

0

It's because when you Change the XP amount, you're changing the XP amount in that function, which once again is firing the XP Changed event, then it will change the XP value and run the event again. This becomes a loop which gets broken by your error.

0

I guess you could use a debounce to fix this.

1 answer

✓ Accepted answer

As M39a9m3R said, the code updates itself into a continuous loop.

I'm going to suggest a Debounce because I know no other way, but I'm sure there are more.

A debounce is a variable used to stop code from running multiple times in a row. To add a debounce would make your code look somewhat like this,

local v = game.Players.LocalPlayer
local Debounce = false
if v.Name == "Player1" or game:GetService("MarketplaceService"):PlayerOwnsAsset(v,440723046)==true then
    if v:FindFirstChild('leaderstats') then
            if v.leaderstats:FindFirstChild('XP') then
                v.leaderstats.XP.Changed:connect(function(z)
                    if Debounce then return end
                    Debounce = true
                    v.leaderstats.XP.Value = v.leaderstats.XP.Value + 25 * workspace.Scripts.Multiplier.Value
                    wait(1)--Shouldn't have to wait long
                    Debounce = false
                end)
            end
    end
end
Wait time could possible be shortened, or may need to be longer.

That was a short was of, hopefully, fixing your code.

I hope I helped.

Good Luck.

If I did help, don't forget to accept my answer.

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