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

Tried to make a shop with a ClickDetector! does not work?

Asked by 8 years ago

Here's the full code.

Price = 50
script.Parent.ClickDetector.MouseClick:connect(function(player)
        local Player = player.Character:FindFirstChild("leaderstats")
    if Player then
        local cash = Player:FindFirstChild("Money")
        if cash then
            if cash.Value >= 50 then
                cash.Value = cash.Value - 50
                local Tool = game.Lightning.Sword:Clone()
                Tool.Parent = game.Lightnings[player.Parent.Name].Backpack
            end
        end
    end


end)

I'd be glad if anyone could help me out!

0
Please accept my answer if I have helped you :) theSkyWars 45 — 8y

1 answer

Log in to vote
1
Answered by 8 years ago

I've changed a few variables in my version of your script as 'Player' when you're looking for their leaderstats will confuse things here.

Line 3

Simple, you can't do that! Therefore we will have to find the player another way, the most simplistic way in my opinion is this:

local Player = game.Players:FindFirstChild(player.Name)
local leaderstats = Player:FindFirstChild("leaderstats")

You simply get the Player's name from them clicking the brick. Then we search through the game.Players for that Player who just clicked the brick. As this is a function we can just do a FindFirstChild, as if it didn't exist it it would not break the rest of the script as it is waiting for nil.

Line 9

local Tool = game.Lightning.Sword:Clone()

There is no such thing as Lightning, therefore it is returning an error as it cannot find game.Lightning. You most likely misspelt this, the correct name is: Lighting.

Line 10

As we located the player through a different way, we can simply just do:

Tool.Parent = Player.Backpack

The Tool

Just incase the item some how is not added into Lighting, or you misspell the item name in the script then I've added a:FindFirstChild.

local Tool = game.Lighting:FindFirstChild("Sword"):Clone()

Conclusion

I have, as previously mentioned at the start of my answer, changed a few of your variables as it was a bit confusing to understand, but I've tested it all out in studio and works perfectly. Just make sure you have the 'Sword' in Lighting.

Price = 50

script.Parent.ClickDetector.MouseClick:connect(function(player)
    local Player = game.Players:FindFirstChild(player.Name)
    local leaderstats = Player:FindFirstChild("leaderstats")
    local Money = leaderstats:FindFirstChild("Money")
        if Money.Value >= 50 then
            Money.Value = Money.Value - 50
            local Tool = game.Lighting:FindFirstChild("Sword"):Clone()
            Tool.Parent = Player.Backpack
    end
end)
Ad

Answer this question