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 9 years ago

Here's the full code.

01Price = 50
02script.Parent.ClickDetector.MouseClick:connect(function(player)
03        local Player = player.Character:FindFirstChild("leaderstats")
04    if Player then
05        local cash = Player:FindFirstChild("Money")
06        if cash then
07            if cash.Value >= 50 then
08                cash.Value = cash.Value - 50
09                local Tool = game.Lightning.Sword:Clone()
10                Tool.Parent = game.Lightnings[player.Parent.Name].Backpack
11            end
12        end
13    end
14 
15 
16end)

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

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

1 answer

Log in to vote
1
Answered by 9 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:

1local Player = game.Players:FindFirstChild(player.Name)
2local 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

1local 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:

1Tool.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.

1local 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.

01Price = 50
02 
03script.Parent.ClickDetector.MouseClick:connect(function(player)
04    local Player = game.Players:FindFirstChild(player.Name)
05    local leaderstats = Player:FindFirstChild("leaderstats")
06    local Money = leaderstats:FindFirstChild("Money")
07        if Money.Value >= 50 then
08            Money.Value = Money.Value - 50
09            local Tool = game.Lighting:FindFirstChild("Sword"):Clone()
10            Tool.Parent = Player.Backpack
11    end
12end)
Ad

Answer this question