Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What is Wrong with this script?

Asked by 8 years ago
Edited 8 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.
01local v = game.Players.LocalPlayer
02if v.Name == "Player1" or game:GetService("MarketplaceService"):PlayerOwnsAsset(v,440723046)==true then
03    if v:FindFirstChild('leaderstats') then
04            if v.leaderstats:FindFirstChild('XP') then
05                v.leaderstats.XP.Changed:connect(function(z)
06                    v.leaderstats.XP.Value = v.leaderstats.XP.Value + 25 * workspace.Scripts.Multiplier.Value
07                end)
08            end
09    end
10end

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

0
Can anyone solve this? advancedev 70 — 8y
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. M39a9am3R 3210 — 8y
0
I guess you could use a debounce to fix this. User#11440 120 — 8y

1 answer

Log in to vote
0
Answered by 8 years ago
Edited 8 years ago

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,

01local v = game.Players.LocalPlayer
02local Debounce = false
03if v.Name == "Player1" or game:GetService("MarketplaceService"):PlayerOwnsAsset(v,440723046)==true then
04    if v:FindFirstChild('leaderstats') then
05            if v.leaderstats:FindFirstChild('XP') then
06                v.leaderstats.XP.Changed:connect(function(z)
07                    if Debounce then return end
08                    Debounce = true
09                    v.leaderstats.XP.Value = v.leaderstats.XP.Value + 25 * workspace.Scripts.Multiplier.Value
10                    wait(1)--Shouldn't have to wait long
11                    Debounce = false
12                end)
13            end
14    end
15end
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.
Ad

Answer this question