I am trying to make a round system for my game based of the tutorial gnomecode did for his game "teddy" but whenever I try to run the game I get an error in my script of "attempting to call a nil value". I have looked everywhere online but have found nothing to help me fix this problem. I bet that this is a simple fix I just don't understand how to fix it.`
Here is my code from the main server script:
local function intermission()
playerModule.respawnAllPlayers()
roundModule.Countdown("Intermission", 5)
end
local function startGame()
playerModule.teleportAllPlayers()
roundModule.Countdown("Game", 3)
end
while true do
intermission()
startGame()
end
Here is my module script code:
local playerModule = {}
local roundInfo = game.ReplicatedStorage:WaitForChild("RoundInfo")
game.Players.PlayerAdded:Connect(function(player)
local marker = Instance.new("StringValue", roundInfo.Players)
marker.Name = player.Name
end)
function playerModule.teleportPlayer(playerName, location)
local gamePlayer = game.Players:FindFirstChild(playerName)
if gamePlayer then
local character = workspace:FindFirstChild(playerName)
if character then
character:WaitForChild("HumanoidRootPart").CFrame = location.CFrame
character.HumanoidRootPart.CFrame = location.CFrame
end
end
function playerModule.teleportAllPlayers(location)
for index, player in pairs(roundInfo.Players:GetChildren()) do
playerModule.teleportPlayer(player.Name, location)
end
end
end
function playerModule.respawnAllPlayers()
for index, player in pairs(roundInfo.Players:GetChildren()) do
local gamePlayer = game.Players:FindFirstChild(player.Name)
if gamePlayer then
gamePlayer:LoadCharacter()
end
end
end
return playerModule
Potentially because you have a ‘end’ below ‘playerModule.teleportPlayer(player.Name, location)’ inside of ‘function playerModule.teleportAllPlayers(location).’ As well as you are missing an ‘end’ inside of ‘function playerModule.teleportPlayer(playerName, location)’ below ‘character.HumanoidRootPart.CFrame = location.CFrame’
I’d attempt that first, as it appears you took if character and removed it from the function. I’d also attempt adding a task.wait() or a wait() inside of your while true loop. The reason why the wait should be added can be found here.
here is my full server side code, I am trying to recreate the Melon Race gamemode from Gmod
--services
local Players = game:GetService("Players")
local Debris = game:GetService("Debris")
--varibles
local Melon = game.ReplicatedStorage.Assets.Melon:WaitForChild("character")
local roundInfo = game.ReplicatedStorage.RoundInfo
local roundModule = require(game.ReplicatedStorage.Modules.Round)
local playerModule = require(game.ReplicatedStorage.Modules.Player)
--setting up player
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local rootPart = character:WaitForChild("HumanoidRootPart")
local humanoid = character:WaitForChild("Humanoid")
local playerMelon = Melon:Clone()
playerMelon.Parent = character
local Weld = Instance.new("Weld")
Weld.Parent = playerMelon
Weld.Part0 = rootPart
Weld.Part1 = playerMelon
humanoid.PlatformStand = true
humanoid.Died:Connect(function()
humanoid.PlatformStand = false
end)
end)
end)
--game logic(round system)
local function intermission()
playerModule.respawnAllPlayers()
roundModule.Countdown("Intermission", 5)
end
local function startGame()
playerModule.teleportAllPlayers(workspace.Spawn)
roundModule.Countdown("Game", 3)
end
while true do
intermission()
startGame()
task.wait()
end
There could be a few reasons; however, since it is calling nil on a value I have reason to believe the moduleScript is erroring out somehow. Im going to review it again rq
local marker = Instance.new("StringValue", roundInfo.Players)
marker.Name = player.Name
and place it inside of the ‘Server’ script and remove the PlayerAdded from your module. This will work because you are already calling PlayerAdded inside of the Server script
Add a print(“module is working”,roundInfo) AFTER the roundInfo.