For loop Failed - lua

I have been trying to make a Roblox sword fight game (my first roblox game).
I found some syntax problems in my code but fixing them didn't fix this issue. I have been going over my code and nothing really seems to work.
Here's the 15th to the 83rd line of code becuase the comments have told me the problem is before the for loop (Before the 15th line are just variables)
--Game Loop
while true do
Status.Value = "Waiting for enough players"
repeat wait(1) until game.Players.NumPlayers >= 2
Status.Value = "Intermission"
wait(10)
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 AvailableMaps = MapsFolder:GetChildren()
local ChosenMap = AvailableMaps[math.random(1,#AvailableMaps)]
Status.Value = ChosenMap.Name.." Chosen"
local ClonedMap = ChosenMap:Clone()
ClonedMap.Parent = workspace
--Teleport players to the map
local SpawnPoints = ClonedMap:FindChild("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 + Vector3.new(0,10,0)
table.remove(AvailableSpawnPoints,1)
-- Give them a 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
Here's the error:
19:30:03.021 - ServerScriptService.Main Script:56: Expected '(', '{' or , got 'for'
Help me out fellow gamers

#luther's comment is absolutely right. The line right above the for loop has a syntax error. SpawnPoints:GetChildren is a function call, and you forgot to add the parentheses.
local AvailableSpawnPoints = SpawnPoints:GetChildren()

Related

Why do my Animations on Roblox Studio not work?

I have a problem with my Roblox combat system. The problem is that my Animations don't play when I play them. Here are the scripts:
Client Script
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local UserInputService = game:GetService("UserInputService")
local Debounce = 0.5
local Keybind = Enum.KeyCode.F
local CanPunch = true
local count = 1
local Animations =
{
script:WaitForChild("PunchAnim"),
script:WaitForChild("PunchAnim2")
}
UserInputService.InputBegan:Connect(function(Input, busy)
if Input.KeyCode == Keybind and not busy then
print("Keybind Check")
if CanPunch == true then
print("CanPunch Check")
CanPunch = false
local Anim = char.Humanoid.Animator:LoadAnimation(Animations[count])
Anim:Play()
Anim.Looped = false
count = (count%#Animations) + 1
print("Anim Played")
game.ReplicatedStorage.remotes.Punch:FireServer(player, char)
print("Fired Event")
wait(Debounce)
CanPunch = true
end
end
end)
Server Script
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)
hitbox = Instance.new("Part", workspace)
hitbox.Size = Vector3.new(4,4,4)
hitbox.CanCollide = false
hitbox.Transparency = 1
local weld = Instance.new("Weld", hitbox)
weld.Part0 = char.HumanoidRootPart
weld.Part1 = hitbox
weld.C1 = CFrame.new(0,0,4)
end)
end)
game.ReplicatedStorage.remotes.Punch.OnServerEvent:Connect(function(player, char)
for i, v in pairs(workspace:GetPartsInPart(hitbox)) do
if v.Parent:FindFirstChild("Humanoid") and v.Parent ~= char and v.Parent:FindFirstChild("Hit"..player.Name) == nil then
local Debounce = Instance.new("IntValue", v.Parent)
Debounce.Name = "Hit"..player.Name
game.Debris:AddItem(Debounce, 0.25)
v.Parent:FindFirstChild("Humanoid"):TakeDamage(7.5)
end
end
end)
In the script, I'm telling the Animations to play after F is pressed but when I press it in the game, it doesn't play the animation.
I made this script by watching various tutorials to make my own combat system. I've tried fixing the code by re-watching the tutorials as well as reading the roblox documentation to find a solution but I couldn't find one.
I'm new to Lua and I apologize in advance if this is a very easy and basic question, but all help is appreciated. Also, please point out any other mistake in my script. Thanks.
If you see this question, please answer it if you can.
The answer of my question was, that the animations were R6 but my character was R15. I went and changed the game settings and made it so the game only supported R6.

Function Triggers even when if statement is false

local Players = game:GetService("Players")
local TouchPart = workspace["Start"]
local target = game.Workspace.Level1
local function teleport()
TouchPart.Touched:Connect(function(touched)
if touched.Parent:FindFirstChild("Humanoid") then
local Player = Players:GetPlayerFromCharacter(touched.Parent)
if Player then
--Player.Character.HumanoidRootPart.CFrame = target.CFrame + Vector3.new(0, 5, 0)
print("Teleport")
end
end
end)
end
while true do
local zeit = game.Workspace.wall1.time.Value
zeit = zeit - 1
game.Workspace.wall1.time.Value = zeit
game.Workspace.wall1.SurfaceGui.Frame.TextLabel.Text = zeit
wait(1)
print(zeit)
if zeit == 0 then
teleport()
game.Workspace.wall1.time.Value = 20
end
end
The Function Teleport should only be called if zeit == 0. And print("Teleport") should only work when zeit hits 0 and the player is touching the part.
My Problem is even if zeit isnt 0 the function teleport() prints "Teleport" into the console when a player touches the part. Am i missing something there ?
Your issue is that you are connecting a Touched listener every time zeit reaches zero. This listener will fire from here on out, until you disconnect it. It also means that after two loops, it will print "teleport" twice, and after three you will see it printed three times with every touch.
You either need to disconnect the listener, or simply teleport the people touching the part when the timer strikes zero. Here's how you would do the latter :
local Players = game:GetService("Players")
local TouchPart = workspace["Start"]
local target = game.Workspace.Level1
local zeit = game.Workspace.wall1.time
local wallText = game.Workspace.wall1.SurfaceGui.Frame.TextLabel
-- function for teleporting all of the players currently touching the TouchPart
local function teleport()
local touchingPlayers = {}
local parts = TouchPart:GetTouchingParts()
for _, touched in ipairs(parts) do
local Player = Players:GetPlayerFromCharacter(touched.Parent)
if Player then
touchingPlayers[Player] = touched.Parent
end
end)
-- teleport all the touching players
for player, character in pairs(touchingPlayers) do
print("Teleporting " .. player.Name)
-- character:PivotTo(target.CFrame + Vector3.new(0, 5, 0))
end
end
-- keep the wall text constantly updated
zeit.Changed:Connect(function(newValue)
wallText.Text = tostring(newValue)
end)
-- update the counter every second
while wait(1) do
zeit.Value -= 1
if zeit.Value == 0 then
teleport()
wait(1)
zeit.Value = 20
end
end

My cash value is suppose to go up every minute but its not. (Roblox Studio lua error)

I was trying to re-edit the code over and over again but it still didn't work I've created the folder leader stats and when I play the game it shows that it's a part of the player. It says however that it isn't a valid member.
The other error says: Cash is not a valid member of Folder "Players.(players name).leaderstats"
It's because game.Players.PlayerAdded is an event which you're assigning to a variable.
Try this for the PlayerAdded script (you will need that cash add function in this script):
local players = []
game.Players.PlayerAdded:Connect(function(ply)
table.insert(players, ply)
end)
while true do
wait(60)
for i, v in ipairs(players) do
v:WaitForChild("leaderstats").Counter.Value += 1
end
end
I've written this from my memory as I am away from a PC that can test this code so best of luck!
while wait(60) do
for i, v in ipairs(game.Players:GetChildren()) do
if v:FindFirstChild("leaderstats") then
if v.leaderstats:FindFirstChild("Counter") then
v.leaderstats.Counter.Value += 1
end
end
end
end
You always need to make sure what you're using exists. If you want to avoid errors, I prefer using FindFirstChild instead of WaitForChild to not get it into an infinite wait incase it somehow doesn't load.
it looks like you forgot to create the leaerstats folder. Here is a fixed code:
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
local Leaderstats = Instance.new("Folder")
Leaderstats.Name = "leaderstats"
Leaderstats.Parent = player
local Counter = Instance.new("IntValue")
Counter.Name = "Counter"
Counter.Parent = Leaderstats
while true do
task.wait(60)
Counter.Value += 1
end
end)
This will start counting time after player joins. If you want to increase counter value of all players at the same time, use this code:
local PlayersService = game:GetService("Players")
local Players = {}
PlayersService.PlayerAdded:Connect(function(player)
local Leaderstats = Instance.new("Folder")
Leaderstats.Name = "leaderstats"
Leaderstats.Parent = player
local Counter = Instance.new("IntValue")
Counter.Name = "Counter"
Counter.Parent = Leaderstats
table.insert(Players, player)
end)
while true do
task.wait(60)
for _, player in ipairs(Players) do
player.leaderstats.Counter.Value += 1
end
end
You don't need to check if "Counter" or "leaderstats" exist as they are created before the player is being inserted into the table.

ServerScriptService.MainScript:88: Expected 'end' (to close 'do' at line 15), got <eof>; did you forget to close 'then' at line 82?

Help me with this please.
-- variebles define
local ReplicatedStorage = game: GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local MapsFolder = ServerStorage:WaitForChild("Maps")
local Status = ReplicatedStorage:WaitForChild("Status")
local GameLength = 50
-- loop of the game
while true do
Status.Value = "Waiting for players (This may take a bit)"
repeat wait(1) until game.Players.NumPlayers >= 2
Status.Value = "Intermission"
wait(15)
local plrs = {}
for i, player in pairs(game.Players:GetPlayers()) do
if player then
table.insert(plrs, player) -- add de playr to de plrs table
end
end
wait(2)
local AvailableMaps = MapsFolder:GetChildren()
["Grass,Snow"]
local AvailableMaps = MapsFolder:GetChildren()
local ChosenMap = AvailableMaps[math.random(1,2)]
Status.Value = ChosenMap.Name.." Chosen"
local ClonedMap = ChosenMap:Clone()
ClonedMap.Parent = workspace
--teleport de playr
local grasstelepors = ClonedMap:FindFirstChild("grasstelepors")
if not grasstelepors then
print("no spawn m8")
end
local Availablegrasstelepors = grasstelepors:GetChildren()
["telepor"]"telepor","telepor","telepor"
for i, player in pairs(plrs) do
if player then
character = player.Character
if character then
--telepor em
character:FindFirstChild("HumanoidRootPart").CFrame = Availablegrasstelepors[1]
table.remove(Availablegrasstelepors,1)
--give them swords so they can violence
local Sword = ServerStorage.Sword:Clone()
Sword.Parent = player.Backpack
local GameTag = Instance.new("BoolValue")
GameTag.Name = "GameTag"
GameTag.Parent = player.Character
else
--no charecter m8
if not player then
table.remove(plrs,i)
end
end
end
end
The error is telling you everything.
The code expected there to be an end to close your while true do on line 15, but instead of finding it, it hit the end of the file.
It is even suggesting where you made the mistake, "did you forget to close your if player then statement on line 82?"
Add an end at the end of the code block to fix it.

Set Humanoid WalkSpeed

I found this script in a similar question but, I'm getting the error 16:11:18.560 - Workspace.Script:2: attempt to index nil with 'Character'
local Player = game:GetService("Players").LocalPlayer
local character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidRootPart = character:WaitForChild("HumanoidRootPart")
-- setting speed
local Humanoid = character:WaitForChild("Humanoid")
if Humanoid then
Humanoid.WalkSpeed = 25
end
Can anyone help me?
The LocalPlayer object only exists in LocalScripts. That's why your variable Player is nil.
There are two ways you can fix this :
1) Move this code into a LocalScript, or
2) Add this code to a callback that is executed when a player joins the game. Here's what that would look like.
local PlayerService = game:GetService("Players")
-- wait for a player to join the game
PlayerService.PlayerAdded:Connect( function(Player)
-- wait for the player's character to load
Player.CharacterAdded:Connect( function(Character)
-- set the speed
local Humanoid = Character:WaitForChild("Humanoid")
if Humanoid then
Humanoid.WalkSpeed = 25
end
end)
end)
Or make a brick that gives you speed:
local KillBrick = script.Parent()
KillBrick.Touched(function(kill)
local humanoid = kill.Parent:FindFirstChild("humanoid")
if humanoid then
humanoid.WalkSpeed = 25
end
end)

Resources