found out of script not updating - roblox lua - 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

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:

Roblox script, doesn't work but doesn't show any errors

I'm making a baldi basics fan game in Roblox, and once you pick up the notebook, it checks when if the player has 7 coins (notebooks, placeholder name) and it doesn't work, it doesn't seem to show any errors in the script editor.
I tried this script:
script.Parent.ClickDetector.MouseClick:Connect(function(plr)
plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value + 1
script.Parent.Sound:Play()
script.Parent:Remove()
game.ServerStorage.artem.Parent = workspace
game.Workspace.lobby:Stop()
game.Workspace.artem_passive.Parent = game.ReplicatedStorage
if plr.leaderstats.Coins.Value >= 7 then -- this is the part that doesn't work
game.Workspace["ready up"]:Play()
game.Workspace["invisible wall"].CanCollide = false
end
end)
There's also a leaderboard script i added in with, that is working.
game.Players.PlayerAdded:connect(function(plr)
local f = Instance.new("Folder", plr)
f.Name = "leaderstats"
local coins = Instance.new("IntValue", f)
coins.Name = "Coins"
coins.Value = 0
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

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

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

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