Why does it keep telling me attempt to index nil with 'wins'? - lua

I have it so it loops through the players to check who's alive or not.
local function checkAlive()
local Player = game.Players:FindFirstChild("leaderstats")
for i,v in pairs(game.Players:GetChildren()) do
if v.Character then
if v.Character.Humanoid.Health > 0 then
Player.wins.Value = Player.wins.Value + 1
end
end
end
But it keeps telling me attempt to index nil with 'wins'?

local Player = game:GetService("Players")
local function checkAlive()
for i,v in pairs(Player:GetPlayers()) do
local leaderstats = v:FindFirstChild("leaderstats")
if v.Character then
if v.Character.Humanoid.Health > 0 then
Player.wins.Value = Player.wins.Value + 1
end
end
end
end

Related

Issue With Moving Character Models In Roblox

I am currently in the process of making a game known as Elf Tycoon. This game involves the player buying elves, (dummy character rigs as placeholders). These characters are then aligned in rows of 8, going on forever. I am having an issue with the system. It seems that every floor apart from the first has its first elf slightly lower than the others. Attached is a screenshot of the issue, and the code is below.
local elvesFolder = script.Parent.Elves
local elfValues = script.Parent.ElfValues
local tycoonLevel2 = script.Parent:FindFirstChild("Tycoon Level 2")
local repStorage = game:GetService("ReplicatedStorage")
local tycoonRoof = script.Parent["Tycoon Roof"]
local currentNumberOfFloors = 1
local currentLevel = 2
local elves = 60
local remainingElves = 60
local number_of_floors = math.floor(elves/9)-1
local tierOneElf = script.Parent.Elves:FindFirstChild("T1 Elf")
local function ResetTycoon()
for i,v in pairs(elvesFolder:GetChildren()) do
v:Destroy()
end
for i,v in pairs(script.Parent:GetChildren()) do
if v.Name == "New Tycoon Level" or v.Name == "Tycoon Level 2" then
v:Destroy()
end
end
end
local function ResetElves()
remainingElves = elves
number_of_floors = math.floor(elves/9)+1
if elves <= 9 then
number_of_floors = 1
end
if number_of_floors >= 2 then
local levelTwo = repStorage:FindFirstChild("Tycoon ".. script.Parent.Name:split(" ")[2] .." Level 2")
if levelTwo then
local newLevel = levelTwo:Clone()
newLevel.Parent = script.Parent
newLevel.Name = "Tycoon Level 2"
end
end
for i = 1,number_of_floors do
if i >= 3 then
local newFloor = tycoonLevel2:Clone()
newFloor.Name = "New Tycoon Level"
newFloor.Parent = script.Parent
for a,b in pairs(newFloor:GetDescendants()) do
if b:IsA("Part") or b:IsA("MeshPart") then
b.Position += Vector3.new(0,7.9*(i-2),0)
end
end
end
for a,b in pairs(tycoonRoof:GetDescendants()) do
if b:IsA("Part") or b:IsA("BasePart") then
b.Position = Vector3.new(b.Position.X,28+7.9*(i-1),b.Position.Z)
end
end
for j = 1,9 do
if j == 1 then
local newElf = repStorage["Tycoon ".. script.Parent.Name:split(" ")[2] .." T1 Elf"]:Clone()
newElf.Name = "T1 Elf"
newElf.Parent = elvesFolder
if i ~= 1 then
for a,b in pairs(newElf:GetDescendants()) do
if b:IsA("Part") or b:IsA("BasePart") then
b.Position += Vector3.new(0,7.9*(i-1),0)
end
end
end
remainingElves -= 1
else
if remainingElves >= 1 then
local newestElf = nil
for a,b in pairs(elvesFolder:GetChildren()) do
newestElf = b
end
local newElf = newestElf:Clone()
newElf.Name = "T1 Elf"
newElf.Parent = elvesFolder
newElf:MoveTo(Vector3.new(newElf.PrimaryPart.Position.X,newElf.PrimaryPart.Position.Y,newElf.PrimaryPart.Position.Z)+(-newElf.PrimaryPart.CFrame.RightVector*6))
end
remainingElves -= 1
end
end
end
end
Note: This is only the first function in the script. This is where the issue would be occurring. Thanks for any help that you can provide!
Here is that screenshot. The expected result is to have all of the elves aligned properly for each floor:

found out of script not updating - roblox lua

I am trying to make it so the GUI updates every time a player collects an item to show how many items they have collected. it works very well, but it only updates when the player leaves and rejoins. Any ideas??
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local catscollected = player:WaitForChild("catsCollected")
Cats_Found_Out_Of = 0
if catscollected:FindFirstChild("sleeping cat") then
Cats_Found_Out_Of = Cats_Found_Out_Of + 1
if catscollected:FindFirstChild("fire cat") then
Cats_Found_Out_Of = Cats_Found_Out_Of + 1
end
end
while true do
script.Parent.Text = Cats_Found_Out_Of.." / 3 Found"
end
It is because of not putting an if statement into the while loop.
Explanation:
Don't use while true do, instead of this use while wait(0.1) do so it will not lag the whole game on client side
It will run only on join and leave because you haven't put the if statement into the while loop. You can either change it to the function or put into the while loop as a code.
Your code should now look like this:
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local catscollected = player:WaitForChild("catsCollected")
while wait(0.1) do
Cats_Found_Out_Of = 0
for _, v in pairs(catscollected:GetChildren()) do
if v.Name == "sleeping cat" or v.Name == "fire cat" then
Cats_Found_Out_Of = Cats_Found_Out_Of + 1
end
end
script.Parent.Text = Cats_Found_Out_Of.." / 3 Found"
end

How to score when killing an NPC

When I kill an NPC, my score should increase, but it does not.
Here's my code. It is located under my NPC model.
local Humanoid = script.Parent.Humanoid
function PwntX_X()
local tag = Humanoid:findFirstChild("creator")
if tag ~= nil then
if tag.Value ~= nil then
local Leaderstats = tag.Value:findFirstChild("leaderstats")
if Leaderstats ~= nil then
Leaderstats.Score.Value = Leaderstats.Score.Value + 250 --Change Money to the stat that is increased.
wait(0.1)
script:remove()
end
end
end
end
Humanoid.Died:connect(PwntX_X)
And here's my code for the leaderboard
game.Players.PlayerAdded:Connect(function(plr)
local stats = Instance.new("BoolValue",plr)
stats.Name = "leaderstats"
local score = Instance.new("IntValue", stats)
score.Name = "Score"
score.Value = 0
end)
Not sure, but looks like
score.Value = Leaderstats.Score.Value

Lua error attempt to index nil value while checking if player has the correct amount of a item

This script allows people to produce meth, starting production will succeed when you have the right amount of objects in your inventory.
Only when you don't have the right amount you should get a notification and you don't get that notification..
Following error pops up: SCRIPT ERROR: #ns-meth/server.lua:18: Attempt to index a nil value.
Code:
RSCore = nil
Citizen.CreateThread(function()
while RSCore == nil do
TriggerEvent('RSCore:GetObject', function(obj) RSCore = obj end)
Citizen.Wait(0)
end
end)
RegisterServerEvent('RSCore_methcar:start')
AddEventHandler('RSCore_methcar:start', function()
local src = source
local Player = RSCore.Functions.GetPlayer(src)
local amount = 0
if Player.Functions.GetItemByName('acetone').amount >= 5 and Player.Functions.GetItemByName('lithium').amount >= 2 and Player.Functions.GetItemByName('methlab').amount >= 1 then
TriggerClientEvent('RSCore_methcar:startprod', src)
Player.Functions.RemoveItem('acetone', 5)
Player.Functions.RemoveItem('lithium', 2)
else
if Player.Functions.GetItemByName('acetone').amount <= 4 and Player.Functions.GetItemByName('lithium').amount <= 1 and Player.Functions.GetItemByName('methlab').amount <= 0 then
RSCore.Functions.Notify("Je hebt niet de juiste benodigdheden!", "error")
end
end
end)
RegisterServerEvent('RSCore_methcar:stopf')
AddEventHandler('RSCore_methcar:stopf', function(id)
local src = source
local Players = RSCore.GetPlayers()
local Player = RSCore.Functions.GetPlayer(src)
for i=1, #Players, 1 do
TriggerClientEvent('RSCore_methcar:stopfreeze', Players[i], id)
end
end)
RegisterServerEvent('RSCore_methcar:make')
AddEventHandler('RSCore_methcar:make', function(posx,posy,posz)
local src = source
local Player = RSCore.Functions.GetPlayer(src)
if Player.Functions.GetItemByName('methlab').amount >= 1 then
local Players = RSCore.Functions.GetPlayer(src)
for i=1, #Players, 1 do
TriggerClientEvent('RSCore_methcar:smoke',Players[i],posx,posy,posz, 'a')
end
else
TriggerClientEvent('RSCore_methcar:stop', src)
end
end)
RegisterServerEvent('RSCore_methcar:finish')
AddEventHandler('RSCore_methcar:finish', function(qualtiy)
local src = source
local Player = RSCore.Functions.GetPlayer(src)
print(qualtiy)
local rnd = math.random(-5, 5)
TriggerEvent('KLevels:addXP', src, 20)
Player.Functions.AddItem('meth', math.floor(qualtiy / 2) + rnd)
end)
RegisterServerEvent('RSCore_methcar:blow')
AddEventHandler('RSCore_methcar:blow', function(posx, posy, posz)
local src = source
local Players = RSCore.GetPlayers()
local Player = RSCore.Functions.GetPlayer(src)
for i=1, #Players, 1 do
TriggerClientEvent('RSCore_methcar:blowup', Players[i],posx, posy, posz)
end
Player.removeInventoryItem('methlab', 1)
end)
RegisterServerEvent('ns-meth:server:callCops')
AddEventHandler('ns-meth:server:callCops', function(streetLabel, coords)
local msg = "Er is een verdachte situatie op "..streetLabel..", mogelijks drugs productie."
local alertData = {
title = "Verdachte situatie",
coords = {x = coords.x, y = coords.y, z = coords.z},
description = msg
}
for k, v in pairs(RSCore.Functions.GetPlayers()) do
local Player = RSCore.Functions.GetPlayer(v)
if Player ~= nil then
if (Player.PlayerData.job.name == "police" and Player.PlayerData.job.onduty) then
TriggerClientEvent("ns-meth:client:robberyCall", Player.PlayerData.source, msg, streetLabel, coords)
TriggerClientEvent("rs-phone:client:addPoliceAlert", Player.PlayerData.source, alertData)
end
end
end
end)

while loop wont work inside of the other while loop

I'm scripting in roblox lua and when I try to put a while loop in the first it messes up the first sequence of code can someone give me hand in fixing this?
The problem is that when the humanoid makes it to tar2 he stops there and wont move I want him to continue running to the points but while also having it so where if a actual person comes by then he runs after him instead.
local larm = script.Parent:FindFirstChild("Left Arm")
local rarm = script.Parent:FindFirstChild("Right Arm")
local plr = game.Players:GetChildren()
local tar1 = game.Workspace.tar1
local tar2 = game.Workspace.tar2
local tar3 = game.Workspace.tar3
local tar4 = game.Workspace.tar4
local pos = {tar1, tar2, tar3, tar4}
local char = script.Parent
local Hum = char.Humanoid
torso = char.Torso
GoTo = 0
while true do
wait(1)
if GoTo == 0 then
Hum:MoveTo(pos[1].Position)
end
if (torso.Position - pos[1].Position).magnitude < 5 then
GoTo = 1
end
if GoTo == 1 then
Hum:MoveTo(pos[2].Position)
end
if (torso.Position - pos[2].Position).magnitude < 5 then
GoTo = 2
end
if GoTo == 2 then
Hum:MoveTo(pos[3].Position)
Goto = 3
end
if (torso.Position - pos[3].Position).magnitude < 5 then
GoTo = 4
end
if GoTo == 5 then
Hum:MoveTo(pos[4].Position)
end
while true do
wait(0.1)
plrs = game.Players:GetChildren()
for i,plr in ipairs(plrs) do
if plr.Character ~= nil then
tor = plr.Character.Torso
if (torso.Position-tor.Position).magnitude <= 5 then
GoTo = 0
Hum:MoveTo(tor.Position)
end
end
end
end
end
Coroutines! Coroutines are the bombdiggety. They let you do multiple while loops at once in a single script.
local larm = script.Parent:FindFirstChild("Left Arm")
local rarm = script.Parent:FindFirstChild("Right Arm")
local plr = game.Players:GetChildren()
local tar1 = game.Workspace.tar1
local tar2 = game.Workspace.tar2
local tar3 = game.Workspace.tar3
local tar4 = game.Workspace.tar4
local pos = {tar1, tar2, tar3, tar4}
local char = script.Parent
local Hum = char.Humanoid
torso = char.Torso
GoTo = 1
function normalCor() -- run around
while true do
wait(1)
for i=1, 4 do
if GoTo == i then
Hum:MoveTo(pos[i].Position)
end
if (torso.Position - pos[i].Position).magnitude < 5 then
GoTo = GoTo + 1
if GoTo == 5 then
GoTo = 1
end
--[[
The above _can_ be done in a single statement:
"GoTo = (GoTo+1)%4+1"
"a % 4" gets the remainder of a / 4 ]]
end
end
end
end
cr = coroutine.create(normalCor)
coroutine.resume(cr) -- this lets you do two while loops at the same time.
function aggroCor() -- when a player is nearby
while true do
wait(0.1)
plrs = game.Players:GetChildren()
for i,plr in ipairs(plrs) do
if plr.Character ~= nil then
tor = plr.Character.Torso
if (torso.Position-tor.Position).magnitude <= 5 then
GoTo = 0
Hum:MoveTo(tor.Position)
end
end
end
end
end
cr = coroutine.create(aggroCor)
coroutine.resume(cr)
Roblox has the syntax spawn(aggroCor) that you can play around with too,
if coroutine.whatever seems like too much work.

Resources