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

how do I make it where if you have 250 energy your inventory is cleared?

local player = game.Players.LocalPlayer
local stats = player:WaitForChild("leaderstats")
local energy = stats:WaitForChild("Energy")

if energy.Value == 250 then
	script.Disabled = false
else
	script.Disabled = true
end
local gear = game.Players.LocalPlayer.Backpack:GetChildren()

for i, child in ipairs(gear) do
	child:Destroy()
	print(game.Players.LocalPlayer.Name.." Inventory Cleared")
end

thats the script. I’m trying to make it where if you have 250 energy then your hammer gets destroyed. But its not working. please help.

3 answers

You can use an Event That fires whenever the value changes ( Has to be an IntValue )

The Changed event fires whenever the IntValue.Value of the IntValue is changed. It will run with the new value being stored in the argument object.

For more Info: https://developer.roblox.com/en-us/api-reference/event/IntValue/Changed

local player = game.Players.LocalPlayer
local stats = player:WaitForChild("leaderstats")
local energy = stats:WaitForChild("Energy")

energy.Changed:Connect(function(NewValue)
	if energy.Value == 250 then
	    script.Disabled = false
	else
    script.Disabled = true
	end
	local gear = game.Players.LocalPlayer.Backpack:GetChildren()
 
	for i, child in ipairs(gear) do
	    child:Destroy()
	    print(game.Players.LocalPlayer.Name.." Inventory Cleared")
	end
end)

5 comments

0

Can you give me more info on how I can do this? The link wont work.

0

Can you give me more info on how I can do this? The link wont work.

0

I edited your script, just copy that, but i rather you learn and understand first instead of just copying

1

it wont work.

0

I fixed the link

For this i would do, since you were operating from ONLY a local script, hence meaning that the bacpack wouldn't clear on the server side and would cause problems later on:

The edited local script

local player = game.Players.LocalPlayer
local stats = player:WaitForChild("leaderstats")
local remoteEvent = game.ReplicatedStorage:WaitForChild("clearEvent")

while wait() do
local energy = stats:WaitForChild("Energy") --Checks every tick for the value "Energy"
if energy.Value == 250 then
     remoteEvent:FireServer() --If the value is 250 then it fires a remote event
 end
 end

And then put a server script wherever you want (preferably serverScriptService) with this code, this will be used to clear the inventory:

local remEvent = Instance.new("RemoteEvent", game.ReplicatedStorage)
remEvent.Name = "clearEvent"

remEvent.OnServerEvent:Connect(function(player) --When the remoteEvent is fired, it executes the code below.
local stats = player:WaitForChild("leaderstats") --This is a check I've put in place so that if exploiters fire the event it also checks for the energy value on the server side.
local energy = stats:WaitForChild("Energy")

if player.leaderstats.Energy == 250 then
local gear = player.Backpack:GetChildren()
for i, child in ipairs(gear) do --This is the part that destroys all of the bacpack's children, I never used loops like these so i re-used the one you made in the local script.
child:Destroy()
print(child.." Has been removed from "..player.." Inventory")
end
end)

(Note: I edited my question due to admin request, now it's easier to understand.)

The problem is that the "remove inventory" part of the code runs as soon as you play, you need to put it in between if energy.Value == 250 then and end

Code:

local player = game.Players.LocalPlayer
local stats = player:WaitForChild("leaderstats")
local energy = stats:WaitForChild("Energy")

if energy.Value == 250 then
    local gear = game.Players.LocalPlayer.Backpack:GetChildren()

    for i, child in pairs(gear) do
        child:Destroy()
        print(game.Players.LocalPlayer.Name .. " Inventory Cleared")
    end
    script.Disabled = false
else
    script.Disabled = true
end

PS: If the player does not have enough energy when the game starts, it will disable the code. Just letting you know!

Accept the answer if it works, comment if it doesn't!

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