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)
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
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:
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.
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.
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