Not giving output When running LocalScript inside of StarterGui - lua

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)

Related

(Help) How to make the script below can be re run after respawn?

So, i am trying to make it can be re run after respawn but it does not work, i am kept
struggling.
My purpose with this script (autofarm, when the tween is done, it does waiting until respawn, when it's respawned, it does re run.)
local function teleport()
local teleport_table = {
location1 = Vector3.new(-70, 69, 83), -- Starter Map
location2 = Vector3.new(-68, 69, 9255), -- Tweening map until above end ches
location3 = Vector3.new(-54, -303, 9496) --to chest
}
local tween_s = game:GetService('TweenService')
local tweeninfo1 = TweenInfo.new(1,Enum.EasingStyle.Linear)
local tweeninfo2 = TweenInfo.new(20,Enum.EasingStyle.Linear)
local tweeninfo3 = TweenInfo.new(1,Enum.EasingStyle.Linear)
local lp = game.Players.LocalPlayer
local function teleport()
if not lp.Character or not lp.Character:FindFirstChild('HumanoidRootPart') then
print("Player character not found")
return
end
function tp3_teleport(v)
if not teleport_table[v] then
print("Invalid location")
return
end
local cf = CFrame.new(teleport_table[v])
local a = tween_s:Create(lp.Character.HumanoidRootPart,tweeninfo3,{CFrame=cf})
a:Play()
while a.PlaybackState == Enum.PlaybackState.Playing do
wait()
end
end
function tp2_teleport(v)
if not teleport_table[v] then
print("Invalid location")
return
end
local cf = CFrame.new(teleport_table[v])
local a = tween_s:Create(lp.Character.HumanoidRootPart,tweeninfo2,{CFrame=cf})
a:Play()
while a.PlaybackState == Enum.PlaybackState.Playing do
wait()
end
tp3_teleport("location3")
end
function tp1_teleport(v)
if not teleport_table[v] then
print("Invalid location")
return
end
local cf = CFrame.new(teleport_table[v])
local a = tween_s:Create(lp.Character.HumanoidRootPart,tweeninfo1,{CFrame=cf})
a:Play()
while a.PlaybackState == Enum.PlaybackState.Playing do
wait()
end
tp2_teleport("location2")
end
tp1_teleport("location1")
end
game.Players.PlayerAdded:Connect(function(player)
if player == game.Players.LocalPlayer then
teleport()
end
end)
I am kept trying to make it run, but also re run after respawn when the script is done and i am kept making and asking ChatGPT which resulted helpless for me.
Changing few last lines to this should work
game.Players.PlayerAdded:Connect(function(player)
if player == game.Players.LocalPlayer then
teleport()
player.CharacterAdded:Connect(function()
teleport()
end)
end
end)

attempt to index nil with 'TookMoney'

local me = script.Parent
local players = game:GetService("Players")
me.Touched:Connect(function(Hit)
local player = players:GetPlayerFromCharacter(Hit.Parent)
if player.TookMoney.Value == true then
player.TookMoney.Value = false
end
end)
It has to make TookMoney.Value = false but it says "attempt to index nil with 'TookMoney' -script:6"
The touched event is triggered on all objects, including non players. Thus players:GetPlayerFromCharacter can return nil. Perform a nil check, e.g.:
me.Touched:Connect(function(Hit)
local player = players:GetPlayerFromCharacter(Hit.Parent)
if player and player.TookMoney.Value == true then
player.TookMoney.Value = false
end
end)
Depending on your setup you may also check if the player has TookMoney.

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.

Atempted to index nil error when trying to find the playergui component of a player

I'm trying to make a cutscene sorta thing for a dorr in roblox studio. My solution was to set up a collision detector on the door which would then make a gui template and set its parent to the playergui component.
I did this using the code
local afterIntoTransform = script.Parent.Parent.DoorUnion.Position.Z -6
local afterOutwardsTransform = script.Parent.Parent.DoorUnion.Position.Z + 6
local debounce = false
local function executeFadeSceneAndTpPlayer(player)
local fadeScene = Instance.new("ScreenGui")
local fadeSceneFrame = Instance.new("Frame")
fadeScene.Name = "fadeScene"
fadeSceneFrame.Name = "fadeFrame"
fadeSceneFrame.Size = UDim2.new(1,0,1,0)
fadeSceneFrame.Parent = fadeScene
fadeSceneFrame.BorderSizePixel = 0
fadeSceneFrame.BackgroundColor3 = Color3.new(1, 1, 1)
fadeSceneFrame.BackgroundTransparency = 1
print(game.Players:GetPlayerFromCharacter(player).Name)
fadeScene.Parent = game.Players:GetPlayerFromCharacter(player).PlayerGui
for i = 0, 20, 1 do
fadeSceneFrame.BackgroundTransparency -= 0.05
wait(0.01)
end
player.HumanoidRootPart.Position = Vector3.new(player.HumanoidRootPart.Position.X, player.HumanoidRootPart.Position.Y, afterOutwardsTransform)
for i = 0, 20, 1 do
fadeSceneFrame.BackgroundTransparency += 0.05
wait(0.01)
end
fadeScene:Destroy()
end
script.Parent.Touched:Connect(function(hit)
if not debounce then
debounce = true
executeFadeSceneAndTpPlayer(hit.Parent)
wait(0.2)
debounce = false
end
end)
It tells me: Attempted to index nil with name on line 15.
It works sometimes and sometimes doesnt but recently Ive noticed a trend that I can walk into the door then out again and then it breaks. I haven't coded in a while so I'm a little rusty but I hope I can get some help.
You are running into the same issue as this person : Roblox - attempt to index nil with 'leaderstats'
You are not accounting for the fact that the Touched event fires for every single part that touches it, and some of those parts might not belong to a player.
You can protect against this error by making sure that the object belongs to a player before you call executeFadeSceneAndTpPlayer()
script.Parent.Touched:Connect(function(hit)
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if not plr then
return
end
if not debounce then
debounce = true
executeFadeSceneAndTpPlayer(hit.Parent)
wait(0.2)
debounce = false
end
end)

Disable lasers instead of being enabled? (ROBLOX 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.

Resources