I am trying to code for a Tower Defense game, and i have run across a problem with the elevators to send players to the map(Technically I am using submarines, not elevators). I used a touched event to make the player go into the sub when they touch a box I made. The script then check to find the next seat not occupied, and makes the player sit there and also disables movement. Then, in a local script in StarterCharacter, I used the seated event to make a leave button appear. A local script in the button makes you leave the seat, destroy the seat weld, and enable controls again. The problem occurs when I try to get back in. The touched event does not fire at all, as there are no errors and i put a print() in the fired function, which didn't print. I also went to a teleporting door I made, which works perfectly fine, and the touched event would not fire after leaving the sub for the first time. Here are the three scripts: In a server script inside the box parts:
Entry = script.Parent Sub = script.Parent.Parent.Submarine debounce = false Entry.Touched:Connect(function(Hit) if debounce == false then debounce = false local Humanoid = Hit.Parent:FindFirstChildWhichIsA("Humanoid") if Humanoid then if Sub.Seat1.Occupant == nil then Sub.Seat1:Sit(Humanoid) if Sub.Parent.ReadyingForDeployment.Value == false then Sub.Parent.ReadyingForDeployment.Value = true end elseif Sub.Seat2.Occupant == nil then Sub.Seat2:Sit(Humanoid) if Sub.Parent.ReadyingForDeployment.Value == false then Sub.Parent.ReadyingForDeployment.Value = true end elseif Sub.Seat3.Occupant == nil then Sub.Seat3:Sit(Humanoid) if Sub.Parent.ReadyingForDeployment.Value == false then Sub.Parent.ReadyingForDeployment.Value = true end elseif Sub.Seat4.Occupant == nil then Sub.Seat4:Sit(Humanoid) if Sub.Parent.ReadyingForDeployment.Value == false then Sub.Parent.ReadyingForDeployment.Value = true end elseif Sub.Seat5.Occupant == nil then Sub.Seat5:Sit(Humanoid) if Sub.Parent.ReadyingForDeployment.Value == false then Sub.Parent.ReadyingForDeployment.Value = true end elseif Sub.Seat6.Occupant == nil then Sub.Seat6:Sit(Humanoid) if Sub.Parent.ReadyingForDeployment.Value == false then Sub.Parent.ReadyingForDeployment.Value = true end elseif Sub.Seat7.Occupant == nil then Sub.Seat7:Sit(Humanoid) if Sub.Parent.ReadyingForDeployment.Value == false then Sub.Parent.ReadyingForDeployment.Value = true end elseif Sub.Seat8.Occupant == nil then Sub.Seat8:Sit(Humanoid) if Sub.Parent.ReadyingForDeployment.Value == false then Sub.Parent.ReadyingForDeployment.Value = true end end wait(0.5) debounce = false end end end)
In a local script inside of StarterCharacter:
Player = game.Players.LocalPlayer Character = game.Players.LocalPlayer.Character Humanoid = Character.Humanoid Humanoid.Seated:Connect(function(active) if active then local Controls = require(game.Players.LocalPlayer.PlayerScripts:WaitForChild("PlayerModule")):GetControls() Controls:Disable() Player.PlayerGui.LeaveSub.Enabled = true end end)
In a local script inside of the leave button:
Button = script.Parent Player = game.Players.LocalPlayer Humanoid = Player.Character:WaitForChild("Humanoid") Button.Activated:Connect(function() local CurrentSub = Humanoid.SeatPart.Parent local TpReference = CurrentSub.TpRefernce local Seat = Humanoid.SeatPart Seat:FindFirstChild("SeatWeld"):Destroy() Humanoid.Parent.HumanoidRootPart.CFrame = CFrame.new(TpReference.Position) Button.Parent.Parent.Enabled = false local Controls = require(game.Players.LocalPlayer.PlayerScripts:WaitForChild("PlayerModule")):GetControls() Controls:Enable() end)
My guess is that the
local Controls = require(game.Players.LocalPlayer.PlayerScripts:WaitForChild("PlayerModule")):GetControls() Controls:Disable()
is the main problem, but at this point I am at a loss.
Any help is appreciated.
Thanks
I think because when your defining the variables your not saying local at all.
Try adding true
inside the Enable parentheses:
Controls:Enable(true)
I also cleaned up the code a bit and gave you two options. First one is more scalable, second one is if you don't mind using a small amount of memory to make things more efficient:
Use this one if you have a ton of objects to check and you don't want to waste memory storing them:
local Entry = script.Parent local Sub = script.Parent.Parent.Submarine local debounce = false Entry.Touched:Connect(function(Hit) if debounce == false then debounce = false local Humanoid = Hit.Parent:FindFirstChildWhichIsA("Humanoid") if Humanoid then for i, v in pairs(Sub:GetChildren()) do --returns a table of all the children objects of Sub if string.sub(v.Name,1,4) == "Seat" then --Checks each children's name and see if it starts with "Seat" if Sub[v.Name].Occupant == nil then --Check each Seat to see if it has an Occupant Sub.Seat1:Sit(Humanoid) if Sub.Parent.ReadyingForDeployment.Value == false then Sub.Parent.ReadyingForDeployment.Value = true end end end end wait(0.5) debounce = false end end end)
Use this one if you don't mind using a little extra memory for very fast looping:
local Entry = script.Parent local Sub = script.Parent.Parent.Submarine local debounce = false local seatTable = { "Seat1", "Seat2", "Seat3", "Seat4", "Seat5", "Seat6", "Seat7", "Seat8" } Entry.Touched:Connect(function(Hit) if debounce == false then debounce = false local Humanoid = Hit.Parent:FindFirstChildWhichIsA("Humanoid") if Humanoid then for i, v in pairs(seatTable) do if Sub[v].Occupant == nil then --Check each Seat to see if it has an Occupant Sub.Seat1:Sit(Humanoid) if Sub.Parent.ReadyingForDeployment.Value == false then Sub.Parent.ReadyingForDeployment.Value = true end end end wait(0.5) debounce = false end end end)
Also, you have a debounce that is never set to anything other than false. To debounce properly, you should set debounce to true as soon as it enters the if statement.
[EDIT] @Turtleraptor372 Efficient position mapping