Currently trying to script a game and I don't understand how to debug this section of coding - debugging

I'm following a youtube tutorial on scripting to create a game on roblox and whilst following it, "Status", the variable I use to identify a value decides to stop working (line39). My output box says the following:
21:16:36.197 - sword game.rbxl auto-recovery file was created
21:16:36.715 - ServerScriptService.MainScript:39: Expected ']' (to close '[' at line 37), got 'Status'
21:16:38.617 - ScriptNavigationHandler : No script currently available.
I haven't learned much about debugging code but if someone can shine some light on what is wrong that would greatly help me on my conquest to learn scripting during the fight against the invisible enemy.
-- Define varibles
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local MapsFolder = ServerStorage:WaitForChild("Maps")
local Status = ReplicatedStorage:WaitForChild("Status")
local GameLength = 60
--Game loop
while true do
Status.Value = "Waiting for enoughplayers"
repeat wait(1) until game.Players.NumPlayers >=2
Status.Value = "Intermission"
wait(8)
local plrs = {}
for i, player in pairs(game.Players:GetPlayers()) do
if player then
table.insert(plrs,player) --Add each player into plrs table
end
end
wait(2)
local AvaliableMaps =MapsFolder:GetChildren()
local ChosenMap = AvaliableMaps[math.random(1,#AvailableMaps)
Status.Value = ChosenMap.Name "Chosen"
local ClonedMap = ChosenMap:Clone()
ClonedMap.Parent = workspace
-- Teleport players to the map
local SpawnPoints = ClonedMap:FindFirstChild("SpawnPoints")
if not SpawnPoints then
print("SpawnPoints not found!")
end
local AvailableSpawnPoints = SpawnPoints:GetChildren()
for i, player in pairs(plrs) do
if player then
character = player.Character
if character then
-- Teleport them
character:FindFirstChild("HumanoidRootPart").CFrame = AvailableSpawnPoints[1].CFrame
table.remove(AvailableSpawnPoints,1)
-- Give Sword
local Sword = ServerStorage.Sword:Clone()
Sword.Parent = player.Backpack
local GameTag = Instance.new("BoolValue")
GameTag.Name = "GameTag"
GameTag.Parent = player.Character
else
-- There is no character
if not player then
table.remove(plrs,i)
end
end
end
end
end

In
local ChosenMap = AvaliableMaps[math.random(1,#AvailableMaps)
you are missing the closing square bracket.

Night94 has pointed out the correct fix for your broken code, but since your question is about learning to debug, I'll try to help you understand your error messages.
21:16:36.715 - ServerScriptService.MainScript:39: Expected ']' (to close '[' at line 37), got 'Status'
Let's break this down piece by piece :
ServerScriptStorage.MainScript:39 this tells us where the file is, and on what line the error appeared.
So let's look at line 39 (through 41) you see :
local ChosenMap = AvaliableMaps[math.random(1,#AvailableMaps)
Status.Value = ChosenMap.Name "Chosen"
Next we have : Expected ']' (to close '[' at line 37), got 'Status'
This means that at some point, the code was expecting a square bracket to close the one that was opened at 37 : AvaliableMaps[, but instead it found the next line of code Status
So with these pieces of information, you should have all the pieces to understand what went wrong: A square bracket was opened, but never closed. It should go somewhere before the next line of code.
local ChosenMap = AvaliableMaps[math.random(1,#AvailableMaps)]

Related

Unable to cast to dictionary - Luau/Roblox Lua

I'm building a game in Roblox and I'm trying to make an NPC go towards the player using PathfindingService, and when the path doesn't work it wanders. However, I keep getting the error message "Unable to cast to Dictionary - Server - Script" and I can't seem to find the source of the issue.
I've checked my code for any tables or dictionaries that may be causing the problem, but I can't seem to find anything. Can anyone help me identify the issue and suggest a possible solution?
local PathfindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local NPC = script.Parent
local TargetPlayer = nil
local Path = nil
local CurrentWaypointIndex = 1
local WaypointReachedDistance = 2
function findTargetPlayer()
local players = Players:GetPlayers()
if #players > 0 then
return players[1]
end
return nil
end
function findPathToTarget()
if not TargetPlayer then
return
end
local humanoid = TargetPlayer.Character and TargetPlayer.Character:FindFirstChild("Humanoid")
if not humanoid then
return
end
local startPosition = NPC.HumanoidRootPart.Position
local endPosition = humanoid.RootPart.Position
Path = PathfindingService:CreatePath(startPosition, endPosition)
Path:ComputeAsync()
if Path.Status == Enum.PathStatus.Success then
CurrentWaypointIndex = 1
else
NPC.Humanoid.WalkToPoint = NPC.HumanoidRootPart.Position + Vector3.new(math.random(-5,5), 0, math.random(-5,5))
end
end
function updateWaypoint()
if not Path or CurrentWaypointIndex > #Path.Waypoints then
return
end
local currentWaypoint = Path.Waypoints[CurrentWaypointIndex]
local distanceToWaypoint = (currentWaypoint - NPC.HumanoidRootPart.Position).Magnitude
if distanceToWaypoint <= WaypointReachedDistance then
CurrentWaypointIndex += 1
end
NPC.Humanoid:MoveTo(NPC.HumanoidRootPart.Position + Vector3.new(math.random(-5,5), 0, math.random(-5,5)))
end
function updateTarget()
TargetPlayer = findTargetPlayer()
if TargetPlayer then
findPathToTarget()
end
end
while true do
updateTarget()
updateWaypoint()
wait(0.1)
end
I've tried various solutions, including changing the variable types and removing potential lists, but nothing has worked so far. I expected the script to run without errors and for the NPC to follow the player.

Roblox Studio: Why does for loop not working on Roblox Studio?

So I am making a CameraScript on roblox studio that when the player touches a robot, the camera focuses on the robot. But the for loop seems to not work though.
The script in game.StarterPlayer.StarterPlayerScripts:
workspace.CurrentCamera.CameraType = Enum.CameraType.Scriptable
game.Players.LocalPlayer.CharacterAdded:Connect(function(char)
local g = char.Name
print(g) --Just for debugging purposes
print("Player Loaded!")
tou(char)
end)
function tou(char)
print("Function had ran")
for _,p in pairs(char:GetChildren()) do
print("We're here loopin ur parts...")
p.Touched:Connect(function(hit)
print("Someone touched?")
if hit.Parent.Name == "Robot" and hit.Parent:IsA("Model") then
print("It's the robot!")
workspace.CurrentCamera.CFrame = hit.Parent.Look.CFrame
workspace.CurrentCamera.Focus = hit.Parent.Head.CFrame
print("Camlock should be successfull...")
else
print("That ain't a robot tho...")
end
end)
end
end
This is the piece of code that doesn't work:
for _,p in pairs(char:GetChildren()) do
print("We're here loopin ur parts...")
p.Touched:Connect(function(hit)
print("Someone touched?")
if hit.Parent.Name == "Robot" and hit.Parent:IsA("Model") then
print("It's the robot!")
workspace.CurrentCamera.CFrame = hit.Parent.Look.CFrame
workspace.CurrentCamera.Focus = hit.Parent.Head.CFrame
print("Camlock should be successfull...")
else
print("That ain't a robot tho...")
end
end)
end
I tried putting the for loop inside directly the CharacterAdded event, putting print() for debugging but it only printed these:
17:55:24.242 <username> - Client - CamLockOnKill:5
17:55:24.243 Player Loaded! - Client - CamLockOnKill:6
17:55:24.243 Function had ran - Client - CamLockOnKill:12
...but it didn't print the others.
It doesn't print We're here loopin ur parts... so the loop isn't run.
The only way to not run a generic for loop like
for _,p in pairs(char:GetChildren()) do
end
without errors is to provide an empty table to pairs.
So char does not have any children. Find out why you think it has children and why it does not.

How to create a Roblox game where the player has to guess a randomly generated pin?

So, I've been working on this for the past week. I have tried everything (based on the knowledge I know) and yet nothing... my code didn't work the first time, the second time, the third time... the forth... etc... at the end, I let frustration take control of me and I ended up deleting the whole script. Luckily not the parts and models, otherwise I'm really screwed...
I need to create a game in which I have to create a keypad of sorts, at first I thought GUI would work... no, it needs to be SurfaceGUI, which I don't know how to handle well... Anyway, I needed to create a keypad using SurfaceGUI, and display it on a separate screen, as a typical keypad would...
The Player would first have to enter an "initial" number, meaning in order to enter the randomly generated number he first needed to input the static pin in order to "log in," after that, then he would try to guess the number...
I've literally tried everything I could but nothing... It's mainly because of my lack of experience in LUA, I'm more advanced in Python and barely know a thing in Java... If someone could assist me on how to do this, I would appreciate it greatly
First, download this and put it in a ScreenGui in StarterGui. Then, use the following LocalScript placed inside the PIN frame:
-- Script settings
local len = 4 -- replace this as needed...
local regen = false -- determines whether PIN will regenerate after a failed attempt
local regmes = "Enter PIN..." -- start message of PIN pad
local badmes = "Wrong PIN!" -- message displayed when PIN is wrong
local success = "Correct PIN!" -- message displayed when PIN is right
-- Script workings
local pin = script.Parent
local top = pin.Top
local txt = top.Numbers
local nums = top.NumKeys
local pin
local stpin
local nms
txt.Text = regmes
local see = game:GetStorage("ReplicatedStorage").PINActivate
local function activate()
if pin.Visible then
pin.Visible = false
for _, btn in pairs(nums:GetChildren()) do
btn.Active = false
end
return
else
pin.Visible = true
for _, btn in pairs(nums:GetChildren()) do
btn.Active = true
end
return
end
end
local function rand()
math.randomseed(os.time) -- better random numbers this way
return tostring(math.floor(math.random(0,9.9)))
end
local function gen()
nms = {rand()}
for i=2, len, 1 do
nms[#nms+1]=rand()
end
stpin = nms[1]
for i=2, #nms, 1 do
stpin = stpin..nms[i]
end
pin = tonumber(stpin) -- converts the number string into an actual number
end
gen()
local function activate(str)
if tonumber(str) ~= pin then
txt.Text = badmes
wait(2)
txt.Text = regmes
if regen then
gen()
wait(0.1)
end
return
else
txt.Text = success
wait(2)
activate()
-- insert code here...
end
end
for _, btn in pairs(nums:GetChildren()) do
btn.Activated:Connect(function()
if txt.Text == "Wrong PIN!" then return end
txt.Text = txt.Text..btn.Text
if string.len(txt.Text) >= len then
activate(txt.Text)
end
wait(0.1)
end)
end
see.OnClientEvent:Connect(activate)
And in a Script put this:
local Players = game:GetService("Players")
local see = game:GetService("ReplicatedStorage").PINActivate
local plr
-- replace Event with something like Part.Touched
Event:Connect(function(part)
if part.Parent.Head then
plr = Players:GetPlayerFromCharacter(part.Parent)
see:FireClient(plr)
end
end)
What this will do is bring up a ScreenGui for only that player so they can enter the PIN, and they can close it as well. You can modify as needed; have a great day! :D
There is an easier way, try this
First, Create a GUI in StarterGui, then, Create a textbox and postion it, after that, create a local script inside and type this.
local Password = math.random(1000, 9999)
print(Password)
game.ReplicatedStorage.Password.Value = Password
script.Parent.FocusLost:Connect(function(enter)
if enter then
if script.Parent.Text == tostring(Password) then
print("Correct!")
script.Parent.BorderColor3 = Color3.new(0, 255, 0)
Password = math.random(1000, 9999)
game.ReplicatedStorage.Correct1:FireServer()
print(Password)
game.ReplicatedStorage.Password.Value = Password
else
print("wrong!")
print(script.Parent.Text)
script.Parent.BorderColor3 = Color3.new(255, 0, 0)
end
end
end)
That's all in the textbox.
Or if you want a random username script, create a textlabel, then, create a local script in the textlabel and type in this.
local UserText = script.Parent
local Username = math.random(1,10)
while true do
if Username == 1 then
UserText.Text = "John"
elseif Username == 2 then
UserText.Text = "Thomas"
elseif Username == 3 then
UserText.Text = "Raymond"
elseif Username == 4 then
UserText.Text = "Ray"
elseif Username == 5 then
UserText.Text = "Tom"
elseif Username == 6 then
UserText.Text = "Kai"
elseif Username == 7 then
UserText.Text = "Lloyd"
elseif Username == 8 then
UserText.Text = "Jay"
elseif Username == 9 then
UserText.Text = "User"
else
UserText.Text = "Guest"
end
wait()
end
All of those if statments are checking what username has been chosen. I have made a roblox game like this recently, so I just took all the script from the game.
If you want to check out my game, Click Here

vbscript won't read file after 8Mb

I have a file written in vbs that wont read a file after about 8MB. I am currently using "Scripting.FileSystemObject". When I test the code, I notice that it runs fine until line ~79500, thats when the "AtEndOfStream" just results in True. I was looking for documentation, but it seems not to exist.
The code is supposed to show duplicate file information and put it in a separate file, which works well enough till around that line.
This is the section of code giving me the problem (it is the second reading function I have in the code):
Set first = fso.OpenTextFile(filePath + firstFileName)
Set secondFile = fso.OpenTextFile(filePath + secondFileName)
count = 0
countInLine = 0
Do Until secondFile.AtEndOfStream
lineMatches = false
lineOfSecond=secondFile.ReadLine
If count > 79440 Then
MsgBox("first line" & first.AtEndOfStream)
End If
Do Until first.AtEndOfStream
lineOfFirst =first.ReadLine
if lineOfSecond = lineOfFirst Then
lineMatches = True
Exit Do
End If
Loop
If Not lineMatches Then
writeFl.Write(count & "second" & lineOfSecond & vbCrLf)
End If
count = count + 1
Loop

Why does that loop sometimes click randomly on screen?

I have made that loop my self and Iam trying to make it faster, better... but sometimes after it repeat searching for existing... it press random ( i think cuz its not similar to any img iam using in sikuli ) place on the screen. Maybe you will know why.
Part of this loop below
while surowiec_1:
if exists("1451060448708.png", 1) or exists("1451061746632.png", 1):
foo = [w_lewo, w_prawo, w_dol, w_gore]
randomListElement = foo[random.randint(0,len(foo)-1)]
click(randomListElement)
wait(3)
else:
if exists("1450930340868.png", 1 ):
click(hemp)
wait(1)
hemp = exists("1450930340868.png", 1)
elif exists("1451086210167.png", 1):
click(tree)
wait(1)
tree = exists("1451086210167.png", 1)
elif exists("1451022614047.png", 1 ):
hover("1451022614047.png")
click(flower)
flower = exists("1451022614047.png", 1)
elif exists("1451021823366.png", 1 ):
click(fish)
fish = exists("1451021823366.png")
elif exists("1451022083851.png", 1 ):
click(bigfish)
bigfish = exists("1451022083851.png", 1)
else:
foo = [w_lewo, w_prawo, w_dol, w_gore]
randomListElement = foo[random.randint(0,len(foo)-1)]
click(randomListElement)
wait(3)
I wonder if this is just program problem with img recognitions or I have made a mistake.
You call twice the exist method indending to get the same match (the first one in your if statement, the second time to assign it to the value. You ask sikuli to evaluate the image twice, and it can have different results.
From the method's documentation
the best match can be accessed using Region.getLastMatch() afterwards.

Resources