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

Why is a Local Part destroyed or not destroyed depending on a value?

Asked by 1 year ago
Edited 1 year ago

Sorry to ask such a simple question, but I've been scratching my head at this for a while now. I have a script that generates a local part and automatically destroys it when Rebirths, a leaderstats value, is >= 1. Here is the script:

local Player = game.Players.LocalPlayer
local Leaderstats = Player:WaitForChild('leaderstats')
local Rebirths = Leaderstats.Rebirths

local Part = Instance.new('Part')
Part.Parent = game.Workspace
Part.Size = Vector3.new(5, 50, 410)
Part.Position = Vector3.new(80, 0, 0)
Part.Anchored = true
Part.Transparency = 0.5
Part.BrickColor = BrickColor.Red()

wait()

if Rebirths.Value >= 1 then
    print('Milestone')
    Part:Destroy()

end

This is the script, and for some reason after I rebirth for the first time (and ''Rebirths'' changes to 1) nothing happens.

Here is the important part: For some reason, when I change the final If statement to >= 0 INSTEAD of >= 1, it works completely fine, printing Milestone and destroying the Local Part. Why is this?

1 answer

Log in to vote
0
Answered by
HEMAN_9 70
1 year ago

The problem is that your code only checks if it should destroy the part the one time it is first ran. to fix this and get the desired result you could use the event ".Changed" for the rebirth leaderstat. on line 14 you could write: Rebirths.Changed:Connect(function(value)

if value >= 1 then print('Milestone') Part:Destroy() end

end)

this way anytime the value of "Rebirths" changes that function will run and destroy the part if it is greater or equal to 1.

Hope this helps

1
I honestly have no idea how this flew over my head, but thank you. TheGamerGuy82 15 — 1y
Ad

Answer this question