Variables inside a function

Hi guys, I’m trying to make a game where when a player is standing on “part1” AND a player is standing on “part2” then an event happens…Here is my code.

`–PLAYER1 START CODE

local player1Start = game.Workspace.Player1
local player1Ready = false

player1Start.Touched:Connect(function()
player1Ready = true
player1Start.BrickColor = BrickColor.new(“Teal”)
end)

player1Start.TouchEnded:Connect(function()
player1Ready = false
player1Start.BrickColor = BrickColor.new(“Smoky grey”)
end)


–PLAYER2 START CODE

local player2Start = game.Workspace.Player2
local player2Ready = false

player2Start.Touched:Connect(function()
player2Ready = true
player2Start.BrickColor = BrickColor.new(“Teal”)
end)

player2Start.TouchEnded:Connect(function()
player2Ready = false
player2Start.BrickColor = BrickColor.new(“Smoky grey”)
end)


if player1Ready == true then
print(“SUCCESSS”)
end`

As you can see I put that print code at the end to show that the variable isnt being carried outside the function, any ideas how I could make a variable to show that both players are on the parts? Thanks for any help!

You will need a loop or a function to check both when a change is done. I’d recommend a function.

–PLAYER1 START CODE

local player1Start = game.Workspace.Player1
local player1Ready = false

player1Start.Touched:Connect(function()
player1Ready = true
player1Start.BrickColor = BrickColor.new(“Teal”)
CheckConditions()
end)

player1Start.TouchEnded:Connect(function()
player1Ready = false
player1Start.BrickColor = BrickColor.new(“Smoky grey”)
end)

–PLAYER2 START CODE

local player2Start = game.Workspace.Player2
local player2Ready = false

player2Start.Touched:Connect(function()
player2Ready = true
player2Start.BrickColor = BrickColor.new(“Teal”)
CheckConditions()
end)

player2Start.TouchEnded:Connect(function()
player2Ready = false
player2Start.BrickColor = BrickColor.new(“Smoky grey”)
end)

function CheckConditions()
   if player1Ready and player2Ready then
      print("SUCCESS")
   end
end
1 Like

I think this doesnt work yet, try this if doesnt

local player1Start = game.Workspace.Player1
local player1Ready = false

local player2Start = game.Workspace.Player2
local player2Ready = false

function CheckConditions()
   if player1Ready and player2Ready then
      print("SUCCESS")
      -- Your Action
   else
      print("TEST FAILED")
   end
end

player1Start.Touched:Connect(function()
player1Ready = true
player1Start.BrickColor = BrickColor.new(“Teal”)
CheckConditions()
end)

player1Start.TouchEnded:Connect(function()
player1Ready = false
player1Start.BrickColor = BrickColor.new(“Smoky grey”)
CheckConditions()
end)

player2Start.Touched:Connect(function()
player2Ready = true
player2Start.BrickColor = BrickColor.new(“Teal”)
CheckConditions()
end)

player2Start.TouchEnded:Connect(function()
player2Ready = false
player2Start.BrickColor = BrickColor.new(“Smoky grey”)
CheckConditions()
end)
1 Like

I am trying to do it so that I can say 'if gameReady1 AND gameReady2 == true then… (do action)

1 Like

then use that code i sent, u can do ur action after the print(“SUCCESS”)

That’s exactly what this does. If you don’t use triple quote (```) it won’t format your code correctly. I fixed it just copy and paste this:

--PLAYER1 START CODE

local player1Start = game.Workspace.Player1
local player1Ready = false

player1Start.Touched:Connect(function()
	player1Ready = true
	player1Start.BrickColor = BrickColor.new("Teal")
	CheckConditions()
end)

player1Start.TouchEnded:Connect(function()
	player1Ready = false
	player1Start.BrickColor = BrickColor.new("Smokey grey")
end)

--PLAYER2 START CODE

local player2Start = game.Workspace.Player2
local player2Ready = false

player2Start.Touched:Connect(function()
	player2Ready = true
	player2Start.BrickColor = BrickColor.new("Teal")
	CheckConditions()
end)

player2Start.TouchEnded:Connect(function()
	player2Ready = false
	player2Start.BrickColor = BrickColor.new("Smokey grey")
end)

function CheckConditions()
	if player1Ready and player2Ready then
		print("SUCCESS")
	end
end
1 Like


any idea why the screen gui isnt showing up when two players stand on the parts?

Your Code is completly wrong. I will send u a script rn.

1 Like

Since u deleted i guess i should add them back?

No they are still useless, but your code itself is not going to work well in practice. You need to rewrite it. I deleted because @Pegagittt said they would rewrite the script.

1 Like

Oh ok, yeah i have been trying to script this for ages and keep starting again from scratch because i keep encountering massive problems with my script

Ok great, thank you very much!

Dont use Touched Event for Player, use Touched Event for the Part. You code would make many buggs, because it has no secure.

I made a Code rn, Try this Code under me:

Insert your part Location in part1 and part2 Variable.

Make your Part Cancollide = false and Anchored = true

Server:

local part1 = game.Workspace.Part1

local part2 = game.Workspace.Part2

local standingOnPart1 = false

local standingOnPart2 = false

local standingTable = {}

local function startEvent()

	if standingOnPart1 == true and standingOnPart2 == true then
		
		local player1 = game:GetService("Players"):GetPlayerByUserId(part1.Name)
		
		local player2 = game:GetService("Players"):GetPlayerByUserId(part2.Name)

		game.ReplicatedStorage:WaitForChild("RemoteEvent"):FireClient(player1)
		
		game.ReplicatedStorage:WaitForChild("RemoteEvent"):FireClient(player2)
		
		standingOnPart1 = false
		
		standingOnPart2 = false
		
		part1.Name = "part1"
		
		part2.Name = "part2"
		
	end

end

part1.Touched:Connect(function(touched)

	local playerTouchedOrNot = game:GetService("Players"):GetPlayerFromCharacter(touched.Parent)

	if playerTouchedOrNot then

		if standingOnPart1 ~= true and part2.Name ~= playerTouchedOrNot.Name then

			standingOnPart1 = true

			part1.Name = playerTouchedOrNot.UserId

			startEvent()

		end

	end

end)

part1.TouchEnded:Connect(function(touched)

	local playerTouchedOrNot = game:GetService("Players"):GetPlayerFromCharacter(touched.Parent)

	if playerTouchedOrNot then

		if standingOnPart1 ~= false and part1.Name == playerTouchedOrNot.Name then

			standingOnPart1 = false

			part1.Name = "part1"

		end

	end

end)

part2.Touched:Connect(function(touched)

	local playerTouchedOrNot = game:GetService("Players"):GetPlayerFromCharacter(touched.Parent)

	if playerTouchedOrNot then

		if standingOnPart2 ~= true and part1.Name ~= playerTouchedOrNot.Name then

			standingOnPart2 = true

			part2.Name = playerTouchedOrNot.UserId

			startEvent()

		end

	end

end)

part2.TouchEnded:Connect(function(touched)

	local playerTouchedOrNot = game:GetService("Players"):GetPlayerFromCharacter(touched.Parent)

	if playerTouchedOrNot then

		if standingOnPart2 ~= false and part2.Name == playerTouchedOrNot.Name then

			standingOnPart2 = false

			part2.Name = "part2"

		end

	end

end)

Client:

local visibleFrameOrOthers = "Insert your Frame, TextLabel, TextBox or others Location in here, if its a ScreenGui u need to use Enabled instead of Visible."

game.ReplicatedStorage:WaitForChild("RemoteEvent").OnClientEvent:Connect(function()
	
	visibleFrameOrOthers.Visible = true -- If you want to make a ScreenGui Visible then you need to replace `Visible` with `Enabled`.
	
end)
1 Like

Ill try it out now thank you very much

1 Like

Dont forget to make the Part Cancollide = false and Anchored = true

1 Like

It works great, thank you so much for the script I really appreciate your help!

The GUI is still not showing up, do you have any idea why?
snippet 1
snippet 2
snippet 3

Well; from the looks of it, you’re modifying StarterGui which wont replicate to the player’s clients since they all have copies of the GUI in game.Players[name].PlayerGui

You could fire a client event to make the GUI visible; RemoteEvent | Roblox Creator Documentation

1 Like

I tried but it’s not working, do you know whats wrong with this:
snippppppplol1

I edited the Text over me, look at it.