Backpack Script not working

Hello DevForum.

I’m trying to make a Backpack Script for my simulator but it doesn’t work.
So if you can help me, feel free to reply to this post

Here is the script:
local player = game.Players.LocalPlayer

while wait() do
script.Parent.Text = player.leaderstats:WaitForChild(“Strength”).Value…“/”…game.ReplicatedStorage.Values.BackpackMax.Value
end

if player.leaderstats.Strength.Value >= game.ReplicatedStorage.Values.BackpackMax.Value then
game.StarterPack[“1 Pound Weight”].Animation.Disabled = true
else
game.StarterPack[“1 Pound Weight”].Animation.Disabled = false
end

The BackpackMax Value is 10

Hello, next time please use triple back ticks ``` to format your code in between like so

local player = game.Players.LocalPlayer

while wait() do
script.Parent.Text = player.leaderstats:WaitForChild(“Strength”).Value…"/"…game.ReplicatedStorage.Values.BackpackMax.Value
end

if player.leaderstats.Strength.Value >= game.ReplicatedStorage.Values.BackpackMax.Value then
game.StarterPack[“1 Pound Weight”].Animation.Disabled = true
else
game.StarterPack[“1 Pound Weight”].Animation.Disabled = false
end

And for your problem there are two possible problems:

  1. Make sure your script is a local script and is inside a backpack, PlayerScripts, or anywhere explained by the dev API reference because:
--This only works for local players
local player = game.Players.LocalPlayer
  1. Secondly try putting your if statement inside the while wait() do loop, this is so currently your code only checks the strength value once the local script runs.

so try this:

while wait() do
script.Parent.Text = player.leaderstats:WaitForChild(“Strength”).Value…"/"…game.ReplicatedStorage.Values.BackpackMax.Value
if player.leaderstats.Strength.Value >= game.ReplicatedStorage.Values.BackpackMax.Value then
game.StarterPack[“1 Pound Weight”].Animation.Disabled = true
else
game.StarterPack[“1 Pound Weight”].Animation.Disabled = false
end

end

1 Like

Sorry, It doesn’t work for me.

Can you tell us what’s the error?

also, try putting your script inside a ````

Whenever I get 10 strength, I want to disable a script

I think its because you’re disabling the script inside the starterpack not the script on your backpack

So I found the error.

game.StarterPack[“1 Pound Weight”].Animation.Disabled = true

If you put something inside the StarterPack, It will clone that and put the thing inside of every player’s backpack.

You are trying to disable the StarterPack’s Animation script. It won’t work because the script is not inside of the player’s backpack.

Instead of this, you should try :
local weight = game.Players.LocalPlayer.Backpack:FindFirstChild("1 Pound Weight") weight.Animation.Disabled = true else weight.Animation.Disabled = false

I found the solution to the problem. Thank you for all your help

You should also try not to use a while loop while you can use a .Changed or :GetPropertyChangedSignal function.

game.Players.LocalPlayer.leaderstats["Strength"].Changed:Connect(function()
--Whatever you need changed
end)
1 Like