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

A button that removes a tool from backpack and adds 5 to leaderstat value?

What I want : I want a button that when I click it, it removes a tool from my backpack and increases my "Stoks"(leaderstat value) value by 5.

What I get : The button does nothing, I have the tool in my backpack and I click on the button, nothing happens.

local button = game.Workspace.Button.Button
local Backpack = game.Players.LocalPlayer:WaitForChild("Backpack")
local leaderstats = game.Players.LocalPlayer:WaitForChild("leaderstats")
AlthosiVal = game.Workspace.Button.Button.AlthosiVal

function sellTool()
    local Tools = Backpack:GetChildren()
    if #Tools > 0 then
        Tools[1]:Destroy()
        leaderstats.Stoks.Value = leaderstats.Stoks.Value + AlthosiVal
    end
end

button.ClickDetector.MouseClick:connect(sellTool)



1 answer

✓ Accepted answer
local button = game.Workspace.button
local Backpack = game.Players.LocalPlayer:WaitForChild("Backpack")
local leaderstats = game.Players.LocalPlayer:WaitForChild("leaderstats")

function sellTool()
    local Tools = Backpack:GetChildren()
    if #Tools > 0 then-- Make sure we don't try deleting nothing
        Tools[1]:Destroy() -- Removes 1st tool found in the Tools Variable
    	leaderstats.Stoks.Value = leaderstats.Stoks.Value + 5
	end
end

button.ClickDetector.MouseClick:connect(sellTool)

The above is the functioning script. This will only work assuming you have FilteringEnabled off and this script is a LocalScript that is located in either StarterPlayer or StarterGui. Make sure your button variable is pointing to the correct part in Workspace! So if it's named button then you would do button = game.Workspace.button

You're using a LocalScript which must be ran on the client, the Workspace is not the client because everything is replicated to every single player and not ran for just a single person.

If this works, accept the answer! If you have questions or problems, just comment bellow!

5 comments

0

It still doesn't work, origianlly, I did not have a localscript I had a normal one. I fixed that and made it a local script. I also checked to see if FilteringEnabled was off, and it was. Script still doesn't work :(

0

My bad, I fixed the answer.

0

Still doesn't work, I re-edited my code. I placed the localscript into StarterPlayer.

0

Just toss it into StarterGui, I already tested this. Make sure your hierarchy is correct.

0

local Button = a part in Workspace.

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