Hi everyone,
Im working on an Obby minigame, where the first player that manages to complete a level is teleported to the next platform that an Obby connects to and from there selects the new level, with the aim of trying to kill all other remaining players.
The issue with my code - that I cannot manage to find what the cause is - is that players can touch the first platform and create a new platform from there. Theoretically, the first platform should not activate the touched event anymore, the newly created platform should. Therefore, I believe the error is that the script is not updating the part the touched event is connected to.
I have tried editing the variables in my code so that the initial platform does not get activated by the touch event at the bottom of my script anymore, and even made separate folders for the “active” platform that should activate the touched event but none of these solutions have worked.
I apologize if this sounds confusing, please feel free to reply so I can clarify any confusion as I would highly appreciate any assistance
Here is the code:
-- Declaring Services
local ServerStorage = game:GetService("ServerStorage")
-- Declaring game Variables
local ValuesFolder = ServerStorage:FindFirstChild("Values")
local AssetsFolder = ServerStorage:FindFirstChild("Assets")
local PlatformsFolder = game.Workspace:FindFirstChild("Platforms")
local CurrentPlatformValue = ValuesFolder:FindFirstChild("CurrentPlatform").Value
local CurrentPlatform = PlatformsFolder:FindFirstChild(CurrentPlatformValue)
local SpawnPlatform = PlatformsFolder:FindFirstChild("SpawnPlatform")
local RecentWinnerName = ValuesFolder:FindFirstChild("RecentWinnerName").Value
-- Declaring script variables
local isTouched = false --debouncing
function placeNextPlatform(currentLevel)
local platform = AssetsFolder:FindFirstChild("Platform"):Clone()
local newLevel = currentLevel + 1
-- The information below is us defining the new platform's details
platform.Position = Vector3.new(10,0, (70*newLevel)) -- 70 is the increment of each stage. So if you reach level 2, then the platform Z pos will be 140 studs.
platform.Parent = PlatformsFolder -- Making it visible
platform.Name = tostring(newLevel) --
return newLevel
end
PlatformsFolder:FindFirstChild(tostring(CurrentPlatformValue)).Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
if humanoid and humanoid.Health > 0 and hit.Parent.Name ~= RecentWinnerName and isTouched == false then
isTouched = true
RecentWinnerName = hit.Parent.Name -- Now we store the name of the player who just completed the stage so they dont activate the new platform
print(CurrentPlatformValue)
-- This function creates the next platform
local newLevel = placeNextPlatform(CurrentPlatformValue)
CurrentPlatformValue = newLevel
print(RecentWinnerName)
print(CurrentPlatformValue)
-- Updating the variable so the previous platform does not fire this event
CurrentPlatform = PlatformsFolder:FindFirstChild(tostring(CurrentPlatformValue))
CurrentPlatform.Transparency = 0.75 -- for debugging purposes, pls remove later!
local HRP = hit.Parent:FindFirstChild("HumanoidRootPart")
HRP.Position = Vector3.new(10,0, (70*CurrentPlatformValue)) -- teleporting the player that touched the stage to the next platform where they will select the next stage.
isTouched = false -- keep this at the end for debouncing purposes
end
end)