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

why is my weapon unlock local script not working?

Asked by 3 years ago

Hi, I have tried to make a script where when you click on a button to give you a weapon, and you do not have enough cash for it, then its text will change. But if you do have enough cash for it then it will give you the gun, but it is not working. I have put this code into a local script, and the error is: leaderstats is not a valid member of InputObject "InputObject" - Client LocalScript:2

script:

script.Parent.Activated:Connect(function(player)
    if player.leaderstats.Cash.Value == 500 then
        game.ReplicatedStorage.Weapons.Crossbow:Clone().Parent = player:FindFirstChild("Backpack")
        script.Parent.Parent.Visible = false
    else
        if player.leaderstats.Cash.Value <= 500 then
            script.Parent.Text = "You need 500 cash to unlock"
            wait(2)
            script.Parent.Text = "Crossbow"
        end
    end
end)
0
your only firing the first "if then" if the player has exactly 500 cash :p 0hsa 193 — 3y

1 answer

Log in to vote
0
Answered by
Rinpix 639 Moderation Voter
3 years ago

First of all, the Activated event of the TextButton doesn't return the player that activated it: https://developer.roblox.com/en-us/api-reference/event/GuiButton/Activated

Second of all, your first conditional statement checks to see if the player has exactly 500 cash, not if they have 500 cash or greater.

Third of all, your second conditional statement checks to see if they have less than or equal to 500 cash, meaning if they have 500 cash, they wouldn't be able to purchase it.

Fourth of all, you're doing all of this on the client, when you should always handle stuff like buying things and giving players tools on the server. And changing your money or giving yourself a tool from the client wouldn't replicate anyways, meaning other players wouldn't be able to see when the player subtracts money from their own balance or gives themselves a tool.

If you want to initiate a transaction on the client, and process it with the server, you'll have to set up a RemoteEvent.

Create a new RemoteFunction and put it inside of ReplicatedStorage. Change its name to "Purchase." ReplicatedStorage is where you put things that the server and the player can see. The player needs to see it to invoke it, and the server needs to see it to pick up on when the player invokes it.

Put this code inside of your LocalScript:

local replicatedStorage = game:GetService("ReplicatedStorage")
local purchaseRemote = replicatedStorage.Purchase

local button = script.Parent
local frame = button.Parent

button.Activated:Connect(function()
    local successfulPurchase = purchaseRemote:InvokeServer()
end)

Whenever the player clicks on the button, the player calls the InvokeServer function on the Purchase RemoteFunction.

Create a Script and put it inside of ServerScriptService. Also, put your crossbow in ServerStorage, and not in ReplicatedStorage. There is no reason for the crossbow to be replicated to the client if the client hasn't yet purchased it.

Put this code inside of that script:

local replicatedStorage = game:GetService("ReplicatedStorage")
local serverStorage = game:GetService("ServerStorage")

local purchaseRemote = replicatedStorage.Purchase
local crossbow = serverStorage.Crossbow

purchaseRemote.OnServerInvoke = function(player)
    local starterGear = player:FindFirstChild("StarterGear")
    local backpack = player:FindFirstChild("Backpack")

    local leaderstats = player:FindFirstChild("leaderstats")
    if (leaderstats) then
        local cash = leaderstats:FindFirstChild("Cash")
        if (cash) then
            if ((cash.Value >= 500) and (not starterGear:FindFirstChild("Crossbow"))) then
                cash.Value -= 500
                local backpackCrossbow = crossbow:Clone()
                local starterGearCrossbow = crossbow:Clone()

                backpackCrossbow.Parent = backpack
                starterGearCrossbow.Parent = starterGear

                return true
            end
        end
    end
end

This script detects when a player initiates a transaction, and handles it. If they have enough money to buy the crossbow, and they don't already have it, subtract 500 cash from their money, and give it to them. StarterGear is where you put tools that you want players to be automatically given when they respawn. You put one clone of the crossbow in there, and another in their backpack, so that they can start using it right away. If you just put it in their StarterGear, they wouldn't be able to use it until they died and respawned. If you just put it in their Backpack, they'd only have it until they died. If the transaction is successful, return true. if it isn't, return false.

This takes us to the second part of the LocalScript:

local replicatedStorage = game:GetService("ReplicatedStorage")
local purchaseRemote = replicatedStorage.Purchase

local button = script.Parent
local frame = button.Parent

button.Activated:Connect(function()
    local successfulPurchase = purchaseRemote:InvokeServer()

    if (not successfulPurchase) then
        button.Text = "You need 500 cash to unlock"
        wait(2)
        button.Text = "Crossbow"
    end
end)

The variable "successfulPurchase" is going to be equal to whatever the server script returns. The server script will return true if the purchase was successful. If it wasn't, it'll return nil, which just means nothing was returned.

if (not successfulPurchase) then

"not successfulPurchase" will evaluate to true if the purchase was unsuccessful, which will then allow the button to display the text "You need 500 cash to unlock."

Ad

Answer this question