Disable lasers instead of being enabled? (ROBLOX Lua) - lua

Hey stackoverflow community. So I have started working on a "Tycoon" in ROBLOX. I have gathered a couple custom models but I ran into an issue with one of them. Basically, I want a laser gate for each player base. The problem I have is that the lasers from the laser gate are enabled at the beginning of the game. Personally I want them to be disabled at the beginning so they can be enabled manually by the player later on. The reason I am struggling with this is because I am not the one who made the script, however, I have tried solving it but I did not succeed.
script.LazerGateLocalScript.LazerGate.Value = script.Parent -- setting a
value
local configs = script.Parent.Configurations
local lazers = script.Parent.LazerGate -- making it easier to access this
part
local lazersOn = true -- so we know if the lasers are on or off
for i, node in pairs(lazers:GetChildren()) do
for i, component in pairs(node:GetChildren()) do
if component.Name == "Emitter" then
local lazerBeam = component:FindFirstChild("ParticleEmitter")
local lazerColor = configs.LazerColor.Value
lazerBeam.Color = ColorSequence.new(lazerColor, lazerColor) -- Setting the lazer color to the lazer color found in the configurations of this model
elseif component.Name == "Diode" then
component.BrickColor = configs.DiodeColor.Value
local diodeSparkle = component:FindFirstChild("ParticleEmitter")
diodeSparkle.Color = ColorSequence.new(Color3.new(255,255,255), configs.DiodeSparkleColor.Value)
elseif component.Name == "Lense" then
component.BrickColor = configs.DiodeColor.Value
end
end
end
script.Parent.KillBrick.PointLight.Color = configs.LazerColor.Value
game.Players.PlayerAdded:connect(function(player) -- when a player is added
player.CharacterAdded:connect(function() -- and whenever their character is loaded then
local newScript = script.LazerGateLocalScript:Clone() -- clone the local script
newScript.Parent = player.PlayerGui -- and put it in the player's playerGui
end)
end)
function lazerToggle(OnOff) -- this funtionc simply changes whether the lasers are on or off
for i, node in pairs(lazers:GetChildren()) do
for i, component in pairs(node:GetChildren()) do
if component.Name == "Emitter" then
local lazerBeam = component:FindFirstChild("ParticleEmitter")
if OnOff == true then
lazerBeam.Enabled = true
else
lazerBeam.Enabled = false
end
end
end
end
end
script.Parent.KillBrick.Touched:connect(function(obj)
if obj.Parent and obj.Parent:FindFirstChild("Humanoid") and lazersOn == true then
obj.Parent:BreakJoints()
end
end)
script.Parent.LazerToggle.OnServerEvent:connect(function() -- when an event is fired then see if we are to open or close the lid and set a value to do so
if lazersOn == true then
lazerToggle(false)
wait(1.5)
lazersOn = false
else
lazerToggle(true)
wait(0.5)
lazersOn = true
end
end)
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
mouse.Button1Down:connect(function() -- if the player chicks then ...
if script.LazerGate.Value and mouse.Target.Name == "LazerButton" and mouse.Target.Parent.Name == "LazerGateSection" then -- if they click the laser gates button
script.LazerGate.Value.LazerToggle:FireServer() -- fire an event that the script will use
end
end)
Once again, my issue is that I want the lasers to be disabled, not enabled at the beginning. Any help would be greatly appreciated. Also, I am very aware that I will be receiving downvotes for this post (xD). But I'll really appreciate any help!
Thanks in advance,
E.W

From what the code tells me, this should work:
script.LazerGateLocalScript.LazerGate.Value = script.Parent
local configs = script.Parent.Configurations
local lazers = script.Parent.LazerGate
local lazersOn = false
for _, node in pairs(lazers:GetChildren()) do
for _, component in pairs(node:GetChildren()) do
if component.Name == "Emitter" then
local lazerBeam = component:FindFirstChild("ParticleEmitter")
local lazerColor = configs.LazerColor.Value
lazerBeam.Color = ColorSequence.new(lazerColor, lazerColor)
elseif component.Name == "Diode" then
component.BrickColor = configs.DiodeColor.Value
local diodeSparkle = component:FindFirstChild("ParticleEmitter")
diodeSparkle.Color = ColorSequence.new(Color3.new(255,255,255), configs.DiodeSparkleColor.Value)
elseif component.Name == "Lense" then
component.BrickColor = configs.DiodeColor.Value
end
end
end
script.Parent.KillBrick.PointLight.Color = configs.LazerColor.Value
game.Players.PlayerAdded:connect(function(player)
player.CharacterAdded:connect(function()
local newScript = script.LazerGateLocalScript:Clone()
newScript.Parent = player.PlayerGui
end)
end)
function lazerToggle(OnOff)
for _, node in pairs(lazers:GetChildren()) do
for _, component in pairs(node:GetChildren()) do
if component.Name == "Emitter" then
local lazerBeam = component:FindFirstChild("ParticleEmitter")
if OnOff then
lazerBeam.Enabled = true
else
lazerBeam.Enabled = false
end
end
end
end
end
script.Parent.KillBrick.Touched:connect(function(obj)
if obj.Parent and obj.Parent:FindFirstChild("Humanoid") and lazersOn == true then
obj.Parent:BreakJoints()
end
end)
script.Parent.LazerToggle.OnServerEvent:connect(function()
if lazersOn == true then
lazerToggle(false)
wait(1.5)
lazersOn = false
else
lazerToggle(true)
wait(0.5)
lazersOn = true
end
end)
lazerToggle(false);
The code above goes into the first script, the server side script which has a child called LazerGateLocalScript.
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
mouse.Button1Down:connect(function()
if script.LazerGate.Value and mouse.Target.Name == "LazerButton" and mouse.Target.Parent.Name == "LazerGateSection" then
script.LazerGate.Value.LazerToggle:FireServer()
end
end)
And then this code above goes into the local script which is a child of the server script called LazerGateLocalScript
Just if you ask, yes, I know RobloxLua.

Related

Roblox Studio Problems with Coden the UI Elements, what need to to?

Good day,
i have the problem that i wana make my visit ui only visible when i am at spawn so at first i wanted to do it with checking the team but i dont found any solousion on how to check which player is in which and than disable/enable ui for them. So after this i made an bool inside of the ui named Loby and got it to work that it turns on / off based on where you are but the problem is that i dont get the ui working i tried this now for serveral hours but i just dont get it and i dont know why. Its only prints not in Loby else if im in Loby or in a round ;(, i hope someone can help me!, Good day
FyMa2618
local loby = script.Parent.Loby
while loby.Value == false do
print("Not In Lobby")
script.Parent.Enabled = false
wait(.1)
end
while loby.Value == true do
local ready = script.Parent.Loaded
print("Lobby is Active")
script.Parent.Enabled = true
wait(.1)
end
--its a local script in the same position as the Bool.
The round script(nedded for the bool)
local intermission = 25
local roundLength = 45
local inRound = game.ReplicatedStorage.InRound
local staus = game.ReplicatedStorage.Status
-- when round value is changed
inRound.Changed:Connect(function()
if inRound.Value == false then
for i, plr in pairs(game.Players:GetChildren()) do
local char = plr.Character
local humanRoot = char:WaitForChild("HumanoidRootPart")
humanRoot.CFrame = game.Workspace.lobbySpawn.CFrame
end
end
end)
-- changes the status
local function round()
while true do
inRound.Value = false
for i = intermission, 0, -1 do
staus.Value = "Game will start in "..i.." seconds"
wait(1)
end
inRound.Value = true
wait(3)
for i = roundLength, 0, -1 do
wait(1)
staus.Value = "Game will end in "..i.." seconds"
local playing = {}
for i, plr in pairs(game.Players:GetChildren()) do
if plr.Team.Name == "Playing" then
local b = game.StarterGui.SpectateGUI.Loby
b.Value = false
table.insert(playing, plr.Name)
print("inserted player")
end
end
if #playing == 0 then
staus.Value = "Everyone Has Died"
wait(3)
break
end
end
end
end
The Region 3 Code(also needed for the bool)
local RegionPart = game.Workspace.RegionPart
local pos1 = RegionPart.Position - (RegionPart.Size / 2)
local pos2 = RegionPart.Position + (RegionPart.Size / 2)
local region = Region3.new(pos1, pos2)
--[[
local part = Instance.new("Part")
part.Anchored = true
part.Size = region.Size
part.Parent = game.Workspace
part.CanCollide = false
part.Transparency = 0.4
dont worry about this
]]--
while true do
wait()
local partsInRegion = workspace:FindPartsInRegion3(region, nil, 1000)
for i, part in pairs(partsInRegion) do
if part.Parent:FindFirstChild("Humanoid") ~= nil then
local char = part.Parent
local loby = game.StarterGui.SpectateGUI.Loby
loby.Value = true
end
end
end[Starter UI][1]

Not giving output When running LocalScript inside of StarterGui

So, Im tring to make a Mind Control script inside of Roblox. Im using the local script so i can use the UserInputServer Module. But When I Run It(Its inside of StartGui), I doesn't give any output. Here is the source:
--Made By BioShot!--
local UIS = game:GetService("UserInputService")
local Enabled = false
local Active = false
game.Players.PlayerAdded:Connect(function(plr)
print("Player Added!")
UIS.InputBegan:Connect(function(input)
if(input['KeyCode'] == Enum['KeyCode']["E"]) then
if(Enabled == true) then
--Rase Player/Dummy/NPC
local Mouse = plr:GetMouse()
Active = true
while(Active == true) do
local Enemy = Mouse["Target"]
print(Enemy)
Enemy.CFrame = Enemy.CFrame + CFrame.new(0,1,0)
wait(0.5)
end
else
--Drop Player/Dummy/NPC
end
end
end)
UIS.InputEnded:Connect(function(input)
if(input['KeyCode'] == Enum["KeyCode"]["F"]) then
Enabled = true
print("Enabled.")
end
if(input['KeyCode'] == Enum["KeyCode"]["E"]) then
Active = false
end
end)
end)
The statements are only printed after game.Players.PlayerAdded:Connect(function(plr) detects a player being added. Due to the code running in a LocalScript, the player will have already been added by the time the script starts waiting for players. If a second player joins the game, "Player Added!" will indeed be printed in the first player's console.
The player can instead be indexed by using local plr = game:GetService("Players").LocalPlayer.
The script still errors when the player's mouse is pointing to nothing and when trying to change the Enemy's position, but these can be easily fixed. The script then lifts objects upwards by hovering over them and pressing E.
Final code:
--Made By BioShot!--
local UIS = game:GetService("UserInputService")
local Enabled = false
local Active = false
local plr = game:GetService("Players").LocalPlayer
print("Player Added!")
UIS.InputBegan:Connect(function(input)
if(input['KeyCode'] == Enum['KeyCode']["E"]) then
if(Enabled == true) then
--Rase Player/Dummy/NPC
local Mouse = plr:GetMouse()
Active = true
while(Active == true) do
local Enemy = Mouse["Target"]
if Enemy then --Make sure the player's mouse isn't pointing to nothing
print(Enemy)
Enemy.CFrame += Vector3.new(0,1,0)
end
task.wait(0.5) --More accurate than wait(0.5)
end
else
--Drop Player/Dummy/NPC
end
end
end)
UIS.InputEnded:Connect(function(input)
if(input['KeyCode'] == Enum["KeyCode"]["F"]) then
Enabled = true
print("Enabled.")
end
if(input['KeyCode'] == Enum["KeyCode"]["E"]) then
Active = false
end
end)

How to make a base system? -- Roblox Lua

I'm trying to make a system to which when the player joins they are given a base and then no other players can use that base.
Here is my module inside the parented to the script:
local Functions = {}
function Functions.findOpenBase(plr)
local bases = workspace.Bases
for i,v in pairs(bases:GetChildren()) do
if v:IsA("Part") then
print("Searching..")
if plr.alreadyOwnsBase.Value == false then
if v.Owner.Value ~= nil then
print("Base found!")
v.Owner.Value = plr.Name
plr.alreadyOwnsBase.Value = true
else
warn("error")
plr:Kick("error finding base, Please Rejoin.")
end
end
else
print("cannot claim another base")
end
end
end
return Functions
then here is my handler script:
local module = require(script.Functions)
game.Players.PlayerAdded:Connect(function(plr)
local alreadyOwnsBase = Instance.new("BoolValue", plr)
alreadyOwnsBase.Name = "alreadyOwnsBase"
alreadyOwnsBase.Value = false
if plr then
module.findOpenBase(plr)
print(plr.Name)
end
end)
is there any solutions?
To me, module inside the parented to the script:
local Functions = {}
function Functions.findOpenBase(plr)
-- check it first.
if plr.alreadyOwnsBase.Value == false then
local bases = workspace.Bases
for i,v in pairs(bases:GetChildren()) do
print("Searching..")
if v:IsA("Part") then
if v.Owner.Value ~= nil then
print("Base found!")
v.Owner.Value = plr.Name
plr.alreadyOwnsBase.Value = true
-- get it, jump the loop
break
end
else
print("cannot claim another base")
end
end
end
-- and plr.alreadyOwnsBase.Value is the flag of the result
if plr.alreadyOwnsBase.Value == false then
warn("error")
plr:Kick("error finding base, Please Rejoin.")
end
end
return Functions
and the handler script:
local module = require(script.Functions)
game.Players.PlayerAdded:Connect(function(plr)
local alreadyOwnsBase = Instance.new("BoolValue", plr)
alreadyOwnsBase.Name = "alreadyOwnsBase"
alreadyOwnsBase.Value = false
if plr then
module.findOpenBase(plr)
-- check the flag
if plr.alreadyOwnsBase.Value then
print(plr.Name)
end
end
end)

Inf timestop glitch in roblox studio ( ServerScriptService.TheWorldServer.TS:35: attempt to index nil with 'Name')

I keep getting this error The error is only for this code. It causes the timestop to be infinite Does anyone know how to fix it? The error is ServerScriptService.TheWorldServer.TS:35: attempt to index
nil with 'Name' Any help is very appreciated.
I have no idea how to fix this so I help is VERY helpful.
local music = game.Workspace.BackgroundMusic
local model = game.ReplicatedStorage.Stand
local InUse = game.ReplicatedStorage.InUse
local bacon = {}
local G = {}
model.BaconTimeStop.OnServerEvent:Connect(function(player)
local find = workspace:GetDescendants()
if InUse.Value == false then
InUse.Value = true
local Sound = script.TSSound:Clone()
Sound.Parent = workspace
Sound:Play()
music:Pause()
wait(1.8)
local TweenService = game:GetService("TweenService")
local part = game.Lighting.ColorCorrection
local goal = {}
goal.Contrast = -2
local tweenInfo = TweenInfo.new(0.2)
local tween = TweenService:Create(part, tweenInfo, goal)
tween:Play()
for i=1, #find do
local That = find[i]
if That:IsA("Part") or That:IsA("MeshPart") then
if That.Anchored == false and That.Parent.Name ~= player.Name and That.Parent.Parent.Name ~= player.Name and That.Name ~= "Baseplate" and not That.Parent:IsA("Accessory") then
That.Anchored = true
table.insert(bacon, That)
end
end
if That:IsA("ParticleEmitter") then
That.TimeScale = 0
table.insert(G, That)
end
end
wait(3)
InUse.Value = false
local TweenService = game:GetService("TweenService")
local part = game.Lighting.ColorCorrection
local goal = {}
goal.Contrast = 0
local tweenInfo = TweenInfo.new(0.2)
local tween = TweenService:Create(part, tweenInfo, goal)
music:Resume()
tween:Play()
for i=1, #bacon do
bacon[i].Anchored = false
end
table.clear(bacon)
for i=1, #G do
G[i].TimeScale = 1
end
table.clear(G)
end
end)
The error message tells you everything you need to know.
ServerScriptService.TheWorldServer.TS:35: attempt to index nil with
'Name'
This is line 35:
if That.Anchored == false
and That.Parent.Name ~= player.Name
and That.Parent.Parent.Name ~= player.Name
and That.Name ~= "Baseplate"
and not That.Parent:IsA("Accessory") then
You index multiple variables with Name as in .Name (indexing operator .).
One of these values is nil.
That cannot be nil as this would cause errors earlier.
print player, That.Parent, and That.Parent.Parent to find out which of them is nil. Then find out why and fix that. We cannot help you here. If it is a valid state simply avoid indexing or replace it by some default value.

Resetting a script after game round?

When the game timer ends it kills the players & resets the teams and sends them to spawn to choose a Team again... idk how to reset the script to start from the beginning and have reset all the values and functions called... I tried making a copy of the script and destroy the current one with script:Destroy() but doesn't work & continues with the same function so breaks my game when the players choose the teams again & respawn.
-- Get Service Variables
local Teams = game:GetService("Teams")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local plr = game:GetService("Players").LocalPlayer
-- Wait for Child Variables
local TeamResetter = game.ReplicatedStorage.TeamResetter
local TimeCountdown = ReplicatedStorage:WaitForChild("Timer")
--Scripts Resets the entire script after GameTime is up
local function ResetGame(Player,Teams)
local copy = script:Clone()
copy.Parent = script.Parent
script:Destroy()
end
-- Destroy Gate when thieves touch it
game.Workspace.CarGate4.GateRod.Touched:Connect(function(hit,Player)
local h = hit.Parent:FindFirstChild("Humanoid")
if h ~= nil then
local n = hit.Parent
local p = game.Players:FindFirstChild(n.Name)
if p.Team.Name == "Thieves" then
game.Workspace.CarGate4.GateRod:Destroy()
end
end
end)
--Thieves function for winning
--[[humanoid.Seated:Connect(function(active,currentSeat, Player, Team)
if active then
if currentSeat.Name == "DriveSeat" then
if Player.TeamColor == game.Teams.Thieves.TeamColor then
game.StarterGui.ThiefWinScreen.Frame.TextLabel.Script.Disabled = false
end
end
end
end)]]
local function PoliceWinReset(Player,Team)
game.StarterGui.PoliceWinScreen.Frame.TextLabel.Script.Disabled = false
wait(2)
for i,v in pairs(game.Teams.Thieves:GetPlayers()) do
Player.TeamColor = game.Teams.ChoosingTeam.TeamColor
game.StarterGui.ThiefWinScreen.Frame.TextLabel.KillPlayer.Disabled = false
Player.Character:BreakJoints()
game.StarterGui.ChooseTeamGUI.Enabled = true
ResetGame(Player,Teams)
end
for i,v in pairs(game.Teams.Police:GetPlayers()) do
Player.TeamColor = game.Teams.ChoosingTeam.TeamColor
game.StarterGui.PoliceWinScreen.Frame.TextLabel.KillPlayer.Disabled = false
Player.Character:BreakJoints()
game.StarterGui.ChooseTeamGUI.Enabled = true
ResetGame(Player,Teams)
end
end
--Starts Global timer for game after user chooses a team & Police win code
--Resets Player Teams and respawns them back at spawn and have to choose a team again
local function PlayGame(Player, Team)
local timerAmount = 120
local timerText = ""
while timerAmount >= 0 do
TimeCountdown:FireAllClients(timerAmount,timerText)
wait(1)
timerAmount -= 1
if timerAmount == 0 then
PoliceWinReset(Player,Team)
end
end
return timerAmount
end
--Checks wether the user is on the Thieves or Police Teama
local function Thieves_Police(Player, Team)
if Player.TeamColor == game.Teams.Police.TeamColor then
game.StarterGui.ChooseTeamGUI.Enabled = false
game.StarterGui.TimerGUI.Enabled = true
wait(5)
PlayGame(Player, Team)
return Player, Team
elseif Player.TeamColor == game.Teams.Thieves.TeamColor then
game.StarterGui.ChooseTeamGUI.Enabled = false
game.StarterGui.TimerGUI.Enabled = true
wait(5)
PlayGame(Player, Team)
return Player, Team
end
end
--Team Chooser
game.ReplicatedStorage.TeamChooser.OnServerEvent:Connect(function(Player, Team)
local PhysicalTeamColor = Teams:FindFirstChild(Team).TeamColor
Player.TeamColor = PhysicalTeamColor
game.StarterGui.PoliceWinScreen.Enabled = false
game.StarterGui.ThiefWinScreen.Enabled = false
Thieves_Police(Player, Team)
end)
--Gives the Users on the Police Team a Weapon on Spawn
function teamFromColor(color)
for _,t in pairs(game:GetService("Teams"):GetChildren()) do
if t.TeamColor==color then return t end
end
return nil
end
function onSpawned(plr)
local tools = teamFromColor(plr.TeamColor):GetChildren()
for _,c in pairs(tools) do
c:Clone().Parent = plr.Backpack
end
end
function onChanged(prop,plr)
if prop=="Character" then
onSpawned(plr)
end
end
function onAdded(plr)
plr.Changed:connect(function(prop)
onChanged(prop,plr)
end)
end
--Calls the Functions
game.Players.PlayerAdded:connect(onAdded)
You can just wrap the script in a while loop to repeat from the beginning when the round ends. At the end of the loop, right before the end tag, you can reset all the values that are supposed to be reset for the next round.

Resources