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

Why won't the following shop GUI script function? [Solved]

player = script.Parent.Parent.Parent.Parent.Parent.Parent
cost = 0


function checkForBat()
	found = false
	for i, v in pairs(player.Backpack.GetChildren()) do
		if v.Name == "Bat" then
			found = true
		end
	end
	for i, v in pairs(player.Character.GetChildren()) do
		if v.Name == "Bat" then
			found = true
		end
	end
	if found == true then
		return true
	else
		return false
	end
end


script.MouseButton1Click:connect(function()
	leaderstats = player.FindFirstChild("leaderstats")
	   if leaderstats then
		   Money = leaderstats.FindFirstchild("Money")
	    if Money then
		   hasBat = checkForBat()
		      if Money.Value >= cost and hasBat == false then
			   Money.Value = Money.Value - cost
			   Bat = game.Lighting.Bat:Clone()
			   Bat.Parent = player.Backpack
		end
	end
	end
	end)

Nothing happens. The GUI is working perfectly, the script just won't do anything....

** The place link is >http://www.roblox.com/The-Purge-place?id=158668406< if it'll help.**

2 comments

0

You can use game.Players.LocalPlayer instead of the script.Parent.Parent... mess.

0

Thanks.

2 answers

✓ Accepted answer
player = game.Players.LocalPlayer --This only works in a local script.
cost = 0 --assign the cost of the bat here.

function checkForBat()
    local found = false
    for i, v in pairs(player.Backpack:GetChildren()) do
        if v.Name == "Bat" then
            found = true
        end
    end
    for i, v in pairs(player.Character:GetChildren()) do
        if v.Name == "Bat" then
            found = true
        end
    end
    return found
end


GuiButtonPath.MouseButton1Click:connect(function()
	local needsBat = not checkForBat() --needsBat is the opposite of found.
	leaderstats = player:FindFirstChild("leaderstats")
	if leaderstats ~= nil then
		Money = leaderstats:FindFirstchild("Money")
		if Money ~= nil then
			if Money.Value >= cost and needsBat then --if they have the money and they need a bat
				Money.Value = Money.Value - cost --subtract cost from funds
				Bat = game.Lighting.Bat:Clone()
				Bat.Parent = player.Backpack --give them a bat
			end
		end
	end
end)

2 comments

0

Thanks! You're great! One more question, if I was the use a script to hide the backpack, then enable the backpack again, would the tools still be there?

0

I don't know for sure, but my guess would be yes.

Okay, so I noticed that you are not actually using the Methods on the values you call, for instance: Money = leaderstats.FindFirstchild("Money") But FindFirstChild is a method, you have to use colons not a period, that goes for the GetChildren method too.

1 comment

0

Im a bit new to scripting... Not sure what you mean. :/

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