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

How to remove part according to leaderstat value?

Asked by
BryanFehr 133
5 years ago

Hello all, I've been trying to get a part to remove according to if a player is a specific level!

Of course, this will need to be done via a LocalScript I believe, as I only want that player to see if they're below the required level, the block, and if above, then it's gone. Now, this is for a sort of teleporter, and there is a image that will be posted ABOVE my code for this.

For better explanation, if unclear, I'd like to remove a part from the Workspace locally. How could I do this? My attempt is below!!

Any and ALL help is appreciated!

Image: https://gyazo.com/7725ad906e6bc11a79ab95d7b73f2de2

Code:

local player = game.Players.LocalPlayer
local part = script.Parent

if player.leaderstats.xp.Value > 7777 then
    script.Parent:Destroy()
end

2 answers

Log in to vote
1
Answered by 5 years ago

Improvements

Use GetService() when retrieving the Players Service

Issues

Your "part" is in the workspace, but LocalScripts cannot be run in the workspace, only server scripts. If you place the LocalScript into the StarterGui and directly name the parentage of the part you are trying to remove, the part should be removed when the player's xp is high enough.

Revised LocalScript

local player = game:GetService("Players").LocalPlayer
local xp = player:WaitForChild("leaderstats"):WaitForChild("xp")

local part = workspace:WaitForChild("RemovePart")

local function XPChange()
    if xp.Value > 7777 then
        part:Destroy()
    end
end

xp:GetPropertyChangedSignal("Value"):Connect(XPChange)
XPChange()

In the above example, the part i am trying to remove is named "RemovePart" and is parented directly under the workspace

Ad
Log in to vote
0
Answered by
Sulu710 142
5 years ago

I'm not a master scripter and this may not be the best way to do it, but rather than running an infinite loop which is not very practical or speedy, you can use the GetPropertyChangedSignal event. You can alter your script to add it by doing this:

local player = game.Players.LocalPlayer
local part = script.Parent

local function XpChanged()  -- function that checks the player's level
    if player.leaderstats.xp.Value > 7777 then
        script.Parent:Destroy()
    end
end

player.leaderstats.xp:GetPropertyChangedSignal("Value"):Connect(XpChanged)  -- If the players Xp amount is changed, this event will fire

What this does is when the "Value" property of player.leaderstats.xp is changed the script will pick that up and fire the event, running the XpChanged function. This still isn't extremely practical because it will run every time the player's Xp is changed, but it's better than running an infinite loop in the background all the time. If there is a better way than this, I would like to know it as well, but this is the only thing I can think of.

0
This method didn't work either! Thank you for your attempt! BryanFehr 133 — 5y

Answer this question