Both attributes change even tho i want it to change 1

Hello I need help for my script, in my game there is a level where you have to push two boxes to two different green platforms and it teleports you. I have 2 attributes to check if a box is on a green platform, but the problem for me is if 1 box is on the green platform both attributes change to true idk how to fix this can someone help?
this is the script:

local greenPlatform = game.Workspace["lvl 10"].touch or game.Workspace["lvl 10"].touch2

greenPlatform.Touched:Connect(function(hit)
	
	local box = hit.Parent:FindFirstChild("Box")
	local box1 = hit.Parent:FindFirstChild("Box1")
	
	if box then
		
		greenPlatform.Parent:SetAttribute("BoxIn", true)
		
	end
	
	if box1 then
		
		greenPlatform.Parent:SetAttribute("Box1In", true)
		
	end
	
	if greenPlatform.Parent:GetAttribute("BoxIn", true) and green.Parent:GetAttribute("Box1In", true) then

		game.Workspace.GetCharacterHere.Touched:Connect(function(OnHit)

			local Humanoid = OnHit.Parent:FindFirstChild("Humanoid")

			if Humanoid then

				Humanoid.Parent:FindFirstChild("HumanoidRootPart").Position = game.Workspace.Last.spawn.Position

				script:Destroy()

			end

		end)

	end
	
end)

Any help is appreciated!

Can you show the boxes in your explorer?

2 Likes

image

I understand what you are trying to achieve but I don’t really see your code reflecting that.

From what I can see, you only run a Touched connection on one of the two green parts? Also, I don’t see the need for two attributes, I believe there are better ways to go about making this.

I improvised a new system for you that does exactly when you want, memory leak free.

local map = workspace.Map
local goals = map.Goals:GetChildren()

local function OnFinished()
	warn("All of the boxes have been put into a goal ")
	
	-- do anything else here
end

for _, goal in goals do
	local con
	con = goal.Touched:Connect(function(hit)
		if hit.Name ~= "Box" then return end -- Ignore if whatever touched the goal isn't named "Box"
		
		--// Add an attribute to the goal
		goal:SetAttribute("Done", true)
		print(`A box has been put into {goal.Name}`)
		
		--// Disconnect the connection to avoid memory leaks
		con:Disconnect()
		con = nil
		
		--// Check if both goals were touched by a box
		for _, goal in goals do
			if goal:GetAttribute("Done") ~= true then break end -- There's other goals to be filled
			
			--// Both goals have been touched
			OnFinished()
			break
		end
	end)
end


The heirarchy of workspace:
Screenshot 2024-01-02 at 3.46.46 pm

Idk if it was intendet but if I put the ball in goal1 first it teleports me but if I put it in goal2 first it teleports me when I put the ball in goal1: so if I want it to work I need to put the ball in goal2 first.

This is me putting the ball in goal1 without putting the other ball in goal2:

Can you show me the properties (including attribute names) of the boxes?

I don’t see goal 2 in that map…
@voozy_v’s script works fine. Given n number of goals, when a goal is touched, it performs a check to see if all the other n-1 goals are touched. If they are, it means that there are no more goals to score.
Since there is only one goal in your map (n = 1), there are no other goals to check.

1 Like

here can you see the 2nd goal

That shouldn’t be happening. Are you sure Goal1 and Goal2 aren’t stacked on each other? Because the other green part might be a different part that isn’t in the Goals folder.

Also what’s inside the goal parts?

nope, its the right part and inside the goals are just touchinterests that automatically get added if it needs to detect if it gets touched
image

also the Done attribute needs to be in both goals right?

No, you don’t need to do the attributes manually, the script does them already.

Also what happens when you push both boxes to both goals?

it only works if you push the box to the 2nd goal first

this happens if you push the box to the 1st goal first

I looked into my code and I found that there were some minor issues. I fixed it now

local map = workspace.Map
local goals = map.Goals:GetChildren()

local function OnFinished()
	warn("All of the boxes have been put into a goal ")

	-- do anything else here
end

for _, goal in goals do
	local con
	con = goal.Touched:Connect(function(hit)
		if hit.Name ~= "Box" then return end -- Ignore if whatever touched the goal isn't named "Box"

		--// Add an attribute to the goal
		goal:SetAttribute("Done", true)
		print(`A box has been put into {goal.Name}`)

		--// Disconnect the connection to avoid memory leaks
		con:Disconnect()
		con = nil

		--// Check if both goals were touched by a box
		local allTouched = true
		for _, _goal in goals do
			if _goal:GetAttribute("Done") ~= nil then continue end -- There's other goals to be filled
			
			allTouched = false
			break
		end
		
		if allTouched then
			--// Both goals have been touched
			OnFinished()
		end
	end)
end

Sorry for wasting your time.

1 Like

its ok thanks for solving the issue

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.