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

Gun Purchase script is not working?

Asked by 9 years ago
player = script.Parent.Parent.Parent.Parent.Parent
cost = 500

function checkForColt()
    found = false
    for i, v in pairs(player.Backpack:GetChildren()) do
        if v.Name == "Colt" then 
            found = true
        end
    end
    for i, v in pairs(player.Charater:GetChildren()) do
        if v.Name == "Colt" then 
            found = true
        end
    end
    if found == true then
        return true
    else
        return false
    end

end

script.Parent.MouseButton1Click:connect(function()
    leaderstats= player:FindFirstChild("leaderstats")
    if leaderstats then 
        Money = leaderstats:FindFirstChild("Points")
        if Money then
            hasColt = checkForColt()
            if Money.Value >= cost and hasColt == false then
                Money.Value = Money.Value - cost
                NewGun = game.Lighting.Colt:Clone()
                NewGun.Parent = Money.Backpack
            end
        end

    end

end)

My leaderboard stats work fine, it out put's no errors and the colt is correctly labeled. 10+ cookiez to the guy the works it out.

0
Try using two variables instead of just one called found. It can be overwritten by the second do loop which could cause the checking for Colt to not go as planned. Also, I believe you might be using your function checkForColt() wrong by making it a variable. Instead on line 29, just call the function by doing only `checkForColt()` . Then on line 30, after "and", check your *two* new variables. alphawolvess 1784 — 9y

2 answers

Log in to vote
0
Answered by
davness 376 Moderation Voter
9 years ago

You comitted several errors:

player = game.Players.LocalPlayer -- LocalPlayer, LocalScript!
cost = 500

function checkForColt()
    found = false
    for i, v in pairs(player.Backpack:GetChildren()) do
        if v.Name == "Colt" then 
            found = true
        end
    end
    for i, v in pairs(player.Charater:GetChildren()) do
        if v.Name == "Colt" then 
            found = true
        end
    end
    if found then -- You should not put "Condition" == bool
        return true
    else
        return false
    end

end

script.Parent.MouseButton1Click:connect(function()
    leaderstats= player:FindFirstChild("leaderstats")
    if leaderstats then 
        Money = leaderstats:FindFirstChild("Points")
        if Money then
            hasColt = checkForColt()
            if Money.Value >= cost and not hasColt then -- if you want to do a thing when a condition is false, use not.
                Money.Value = Money.Value - cost
                NewGun = game.ServerStorage.Colt:Clone() -- Why did you put the gun on Lighting???? Put it on ServerStorage - it's for that it exists.
                NewGun.Parent = game.Players.LocalPlayer.Backpack -- LocalPlayer, local script.
            end
        end

    end

end)

Well, i'm not a proscripter. But try to put it inside a LocalScript instead of a normal one (I guess you used the normal one) - Do not get mad if it do not work. I tryed at least.

0
If it's in a GUI, it should be a LocalScript. Also, LocalScripts don't work for ServerStorage, but they do work for ReplicatedStrorage. alphawolvess 1784 — 9y
0
I have just noticed in the leaderstats I have money rather than points XD WolfgangVonPrinz 0 — 9y
0
Wolfgang, i need the hierachy of the game where the script is davness 376 — 9y
Ad
Log in to vote
0
Answered by 9 years ago
player = game.Players.LocalPlayer

cost = 500



function checkForColt()

    found = false

    for i, v in pairs(player.Backpack:GetChildren()) do

        if v.Name == "Colt" then

            found = true

        end

    end

    for i, v in pairs(player.Charater:GetChildren()) do

        if v.Name == "Colt" then

            found = true

        end

    end

    if found then 

        return true

    else

        return false

    end



end


script.Parent.MouseButton1Click:connect(function()

    leaderstats= player:FindFirstChild("leaderstats")

    if leaderstats then

        Money = leaderstats:FindFirstChild("Money")

        if Money then

            hasColt = checkForColt()

            if Money.Value >= cost and not hasColt then 
                Money.Value = Money.Value - cost
             NewGun = game.ReplicatedStorage.Colt:Clone() 

                NewGun.Parent = game.Players.LocalPlayer.Backpack -- LocalPlayer, local script.
          end

        end

    end

end)

So, I've re-written, but it read's errors I don't know how to solve.

"Charater is not a valid member of player." I tried Humanoid too, but no luck. Those cookies are still up for grabs!

Answer this question