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 7 years ago
Edited 7 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.
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

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

1 answer

Log in to vote
0
Answered by 7 years ago
Edited 7 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,

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.
Ad

Answer this question