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

Another question about bools!?!

So I am trying to make a dropper that when purchased uses a bool to tell my dropper to give mere money, here is my code

function onClicked(playerWhoClicked)
	if Mybool then  --The Mybool in this phrase has an error stating that Mybool is a unknown global
	script.Parent.BrickColor = BrickColor.Red()
	local points = playerWhoClicked.leaderstats.Copper
	points.Value = points.Value-1
	local debrisServ = game:GetService('Debris')
	local blockLife = 1
	local dropper = Instance.new('Part', game.Workspace)
	dropper.Anchored = false
	dropper.CFrame = CFrame.new(-133, 9, 93)
local dropPart = Instance.new('Part')
dropPart.Color = Color3.new(0,0,0)
wait(1.2)
local points = playerWhoClicked.leaderstats.Money
points.Value = points.Value+7
wait(4)
script.Parent.BrickColor = BrickColor.Green()

	else
		script.Parent.BrickColor = BrickColor.Red()
	local points = playerWhoClicked.leaderstats.Copper
	points.Value = points.Value-1
	local debrisServ = game:GetService('Debris')
	local blockLife = 1
	local dropper = Instance.new('Part', game.Workspace)
	dropper.Anchored = false
	dropper.CFrame = CFrame.new(-133, 9, 93)
local dropPart = Instance.new('Part')
dropPart.Color = Color3.new(0,0,0)
wait(1.2)
local points = playerWhoClicked.leaderstats.Money
points.Value = points.Value+5
wait(4)
script.Parent.BrickColor = BrickColor.Green()
	end
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

That was the dropper this is the upgrader

Mybool = false
function onClicked(playerWhoClicked)
	Mybool = true --This should technically make Mybool a global but it does not
	game.Workspace.B1.Transparency =100
	game.Workspace.PA.Transparency =0
	game.Workspace.PB.Transparency =0
	game.Workspace.PD.Transparency =0
	game.Workspace.PE.Transparency =0
	local points = playerWhoClicked.leaderstats.Money
points.Value = points.Value-20
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

This all works fine, but my droppers value of cash for the ore will not change, sorry if my last question seems similar but I cannot find a way around it.

2 answers

✓ Accepted answer

If I understand correctly, you are trying to reference a variable from one script, inside another. A global variable just means it can be accessed by any block/indentation inside a certain script. If you want to re-use variables through multiple scripts, check out modulescripts. Or you can use a variable object, and reference inside a script.

Here's the first way to fix it that I can think of. If you create a folder inside each player as they join and create a BoolValue inside, you can reference the BoolValue from a script, and have it be accessible from anywhere (so long as FE is off).

Here's a list of stuff you might need: PlayerAdded Instance.new BoolValue

To reference a BoolValue that's in a player, you might use something like local boolval = player.Values.BoolValue. Whereas the 'Values' is the folder, the 'player' is the player object, and of course the 'BoolValue' is the value you are referencing.

OR

as FrostTaco pointed out, and I forgot completely about, as the fact that you can use '_G'. '_G' is a table variable that is shared between all scripts, and can be assigned many different things. Including other tables and functions. Beware of using it in localscripts though, as they are counted as separate Lua instances. _G

7 comments

0

Which function do you recommend I use I have tried a few that I thought were correct and came up with the same error

0

Oh, duh. You can also use _G which makes an ACTUAL global variable. You'd have to figure out how to verify which player you are referencing though.

0

So I would say: Mybool = true_G???

0

No, sorry for not being clear. It's _G.MyBool = true. As FrostTaco state.

0

Thedestroyer, please read my answer and it tells you.

0

THANKS SO MUCH! that question was on my nerves for SOOOOOO long and you solverd it with the _G. thing thanksss!

0

Thedestroyer, you don't have to change it, but I'm just saying I was the original one to post the _G, and I kinda like my reputation. xD Again, if you don't want to, then don't change it :P

Everytime you have the word: Mybool, replace it with _G.Mybool

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