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

Gun Purchase script is not working?

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.

1 comment

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.

2 answers

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.

3 comments

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.

0

I have just noticed in the leaderstats I have money rather than points XD

0

Wolfgang, i need the hierachy of the game where the script is

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!

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