Issue With Moving Character Models In Roblox - lua

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:

Related

Part doesn't gives damage with Touched Event

I made a code for my game, it's a type of punch, with a hitbox made with a common Part. I proggrammed to the part give some damage according with the player strenght. It didn't work, no errors in output. Code here:
local rep = game:GetService("ReplicatedStorage")
local debounceDMG = true
rep.Combate.Soco.OnServerEvent:Connect(function(plr)
local math = math.random(1,2)
local char = plr.Character or plr.CharacterAdded:Wait()
local Humanoid = char.Humanoid
local lanim = char.Humanoid:LoadAnimation(script.Left)
local lanim1 = char.Humanoid:LoadAnimation(script.Animation)
local hitbox = Instance.new("Part")
hitbox.Parent = workspace
hitbox.Transparency = 1
hitbox.Anchored = true
hitbox.CanCollide = false
hitbox.Size = Vector3.new(2.5,2.5,2.5)
game:GetService("RunService").Heartbeat:Connect(function()
if math == 2 then
hitbox.Position = char.LeftHand.Position
elseif math == 1 then
hitbox.Position = char.RightHand.Position
end
end)
if math == 2 then
lanim:Play()
elseif math == 1 then
lanim1:Play()
end
hitbox.Touched:Connect(function(hit)
local h = hit.Parent:FindFirstChild("Humanoid")
if h then
if h ~= Humanoid and debounceDMG then
debounceDMG = false
h.Health -= plr.Data.Forca.Value + 5
task.wait(.5)
debounceDMG = true
end
end
end)
task.wait(.3)
hitbox:Destroy()
end)
You use "if h~=Humanoid" will always look for Non Humanoid, your code will never reach the Health deduction. Try to code below if working
hitbox.Touched:Connect(function(hit)
local h = hit.Parent:FindFirstChild("Humanoid")
if h ~= nil and debounceDMG then
debounceDMG = false
h.Health -= plr.Data.Forca.Value + 5
task.wait(.5)
debounceDMG = true
end
end)

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)

Equivalent of x++ in Lua?

I am trying to do
local a = 1
for i,v in pairs(getloadedmodules()) do
if v:IsA("ModuleScript") then
v.Name = a = a + 1
end
end
But obviously that doesn't work because of the = sign after a. Is there any workaround to this?
local a = 1
for i,v in pairs(getloadedmodules()) do
if v:IsA("ModuleScript") then
a = a + 1
v.Name = a
end
end

Not doing the random pet, just giving the rarest every time. How do I fix it?

I am making a simulator game on roblox and it has a pet system. I revamped how the hatch works, and for some reason it started to glitch out, and always give the same pet, and the pet was always the rarest one. I don't know what the problem is, here is the script. The Weight system worked perfectly before, but now it isnt working. I compared it to the old version of the game and the only difference is what the variables are called.
game.ReplicatedStorage.OpenEgg.OnServerInvoke = (function(player, amount, egg)
print("Fired")
if not player:FindFirstChild("Debounce") then
if amount == "One" then
local CC = player.leaderstats.CloudCoins
local EO = player.leaderstats.EggsOpened
local EGGS = require(game.ReplicatedStorage.EGGS)
local Data = EGGS[egg]
local Price = Data["Cost"]
local Pets = Data["Pets"]
local TotalWeight = 0
for i,v in pairs(Pets) do
TotalWeight = TotalWeight + v[1]
end
local function ChoosePet(player)
local Chance = math.random(1,TotalWeight)
local Counter = 0
for d,c in pairs(Pets)do
Counter = Counter + c[1]
if Chance >= Counter then
return d
end
end
end
local function GetChance(ChosenPet)
for i, v in pairs(Pets)do
for o,b in pairs(Pets) do
if o == ChosenPet then
return b[1]
end
end
end
end
if CC.Value >= Price then
local ChosenPet = ChoosePet(player)
local Chance = GetChance(ChosenPet)
CC.Value = CC.Value - Price
EO.Value = EO.Value + 1
local Pet = Instance.new("BoolValue")
Pet.Name = ChosenPet
Pet.Parent = player.Pets
if not player.PetsA:FindFirstChild(Pet.Name.." Amount") then
local AmountA = Instance.new("IntValue")
AmountA.Name = Pet.Name.." Amount"
AmountA.Value = 1
else
local AmountA = player.PetsA[Pet.Name.." Amount"]
AmountA.Value = AmountA.Value + 1
end
local Level = Instance.new("IntValue")
local Xp = Instance.new("IntValue")
local Number = Instance.new("IntValue")
Number.Name = "Number"
Number.Value = player.PetsA[Pet.Name.." Amount"].Value
Number.Parent = Pet
Level.Name = "Level"
Xp.Name = "XP"
Level.Value = 1
Xp.Value = 0
Level.Parent = Pet
Xp.Parent = Pet
return("Hatched")
else
return("NotEnoughCoins")
end
end
if amount == "Triple" then
local CC = player.leaderstats.CloudCoins
local EO = player.leaderstats.EggsOpened
local EGGS = require(game.ReplicatedStorage.EGGS)
local Data = EGGS[egg]
local Price = Data["Cost"]
local Pets = Data["Pets"]
local TotalWeight = 0
for i,v in pairs(Pets) do
TotalWeight = TotalWeight + v[1]
end
local function ChoosePet(player)
local Chance = math.random(1,TotalWeight)
local Counter = 0
for d,c in pairs(Pets)do
Counter = Counter + c[1]
if Chance >= Counter then
return d
end
end
end
local function GetChance(ChosenPet)
for i, v in pairs(Pets)do
for o,b in pairs(Pets) do
if o == ChosenPet then
return b[1]
end
end
end
end
if CC.Value >= Price*3 then
local ChosenPet1,ChosenPet2,ChosenPet3 = ChoosePet(player),ChoosePet(player),ChoosePet(player)
local Chance1,Chance2,Chance3 = GetChance(ChosenPet1),GetChance(ChosenPet2),GetChance(ChosenPet3)
CC.Value = CC.Value - Price*3
EO.Value = EO.Value + 3
local Pet1 = Instance.new("BoolValue")
Pet1.Name = ChosenPet1
Pet1.Parent = player.Pets
local Level1 = Instance.new("IntValue")
local Xp1 = Instance.new("IntValue")
local Number1 = Instance.new("IntValue")
Number1.Name = "Number"
if not player.PetsA:FindFirstChild(Pet1.Name.." Amount") then
local AmountA = Instance.new("IntValue")
AmountA.Name = Pet1.Name.." Amount"
AmountA.Value = 1
else
local AmountA = player.PetsA[Pet1.Name.." Amount"]
AmountA.Value = AmountA.Value + 1
end
Number1.Value = player.PetsA[Pet1.Name.." Amount"].Value
Number1.Parent = Pet1
Level1.Name = "Level"
Xp1.Name = "XP"
Level1.Value = 1
Xp1.Value = 0
Level1.Parent = Pet1
Xp1.Parent = Pet1
local Pet2 = Instance.new("BoolValue")
Pet2.Name = ChosenPet2
Pet2.Parent = player.Pets
local Level2 = Instance.new("IntValue")
local Xp2 = Instance.new("IntValue")
local Number2 = Instance.new("IntValue")
Number2.Name = "Number"
if not player.PetsA:FindFirstChild(Pet2.Name.." Amount") then
local AmountA = Instance.new("IntValue")
AmountA.Name = Pet2.Name.." Amount"
AmountA.Value = 1
else
local AmountA = player.PetsA[Pet2.Name.." Amount"]
AmountA.Value = AmountA.Value + 1
end
Number2.Value = player.PetsA[Pet2.Name.." Amount"].Value
Number2.Parent = Pet2
Level2.Name = "Level"
Xp2.Name = "XP"
Level2.Value = 1
Xp2.Value = 0
Level2.Parent = Pet2
Xp2.Parent = Pet2
local Pet3 = Instance.new("BoolValue")
Pet3.Name = ChosenPet3
Pet3.Parent = player.Pets
local Level3 = Instance.new("IntValue")
local Xp3 = Instance.new("IntValue")
local Number3 = Instance.new("IntValue")
Number3.Name = "Number"
if not player.PetsA:FindFirstChild(Pet3.Name.." Amount") then
local AmountA = Instance.new("IntValue")
AmountA.Name = Pet3.Name.." Amount"
AmountA.Value = 1
else
local AmountA = player.PetsA[Pet3.Name.." Amount"]
AmountA.Value = AmountA.Value + 1
end
Number3.Value = player.PetsA[Pet3.Name.." Amount"].Value
Number3.Parent = Pet3
Level3.Name = "Level"
Xp3.Name = "XP"
Level3.Value = 1
Xp3.Value = 0
Level3.Parent = Pet3
Xp3.Parent = Pet3
return("Hatched")
else
return("NotEnoughCoins")
end
end
end
end)
take a closer look at these lines of code.
local TotalWeight = 0
for i,v in pairs(Pets) do
TotalWeight = TotalWeight + v[1]
end
local function ChoosePet(player)
local Chance = math.random(1,TotalWeight)
local Counter = 0
for d,c in pairs(Pets)do
Counter = Counter + c[1]
if Chance >= Counter then
return d
end
end
end
the next time we see this function it is to assign the chosen pet to the player. I would suggest playing around with the Chance value. Set it to something crazy and see if you get another pet instead.

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