Equation script for scaling rebirth cost not working - lua

So basically I'm making a roblox clicker game where I have rebirths which have a cost and i made a menuOrganiser script that basically automatically makes rebirth buttons every time the value of players.upgrades.rebirthbuttons changes and I have added an equation that should scale the cost every time I rebirth but for someone it doesn't change the cost on the actual screen it says that the cost is 800 but when you try to rebirth with 800 clicks it doesn't work so it changes it but not the GUI and I'm wondering why.
This is the script with the equation for the rebirths. And I also get this error:
Players.kukata4321.PlayerGui.ScreenGui.RebirthMenu.ScrollingFrame.menuOrganiser:55: attempt to index number with 'Value'
...
rebirthButtons.Changed:Connect(function()
local amount = rebirthButtons.Value
for count = 1, amount do
if not scrollingframe:FindFirstChild(rebirthOptions[count]) then
local newButton = template:Clone()
local rebirthsAmount = rebirthOptions[count]
newButton.Name = rebirthsAmount
newButton.rebirthAmount.Text = rebirthsAmount.." Rebirths"
newButton.cost.Text = rebirthsAmount*800*((1)+((rebirths.Value)/10)).. " Clicks"
newButton.Parent = scrollingframe
end
end
end)
rebirths.Changed:Connect(function()
local rebirths = leaderstats:WaitForChild("Rebirths").Value
for _, child in pairs(scrollingframe:GetChildren()) do
if child:IsA("TextButton") then
local rebirthsAmount = tonumber(child.Name)
child.cost.Text = rebirthsAmount*800*((1)+((rebirths.Value)/10)).. " Clicks"
end
end
end)

So what your error means is that you defined a variable with an IntValue's value (which is a number) and then tried to get the "Value" of a number
Eg:
local rebirths = game.Players.leaderstats.Rebirths.Value -- returns a number
print(rebirths.Value) -- 12345.Value doesn't work
The simple solution is to remove .Value from when you define the variable
rebirths.Changed:Connect(function()
local rebirths = leaderstats:WaitForChild("Rebirths") -- Remove .Value
for _, child in pairs(scrollingframe:GetChildren()) do
if child:IsA("TextButton") then
local rebirthsAmount = tonumber(child.Name)
child.cost.Text = rebirthsAmount*800*((1)+((rebirths.Value)/10)).. " Clicks"
end
end

Related

attempt to index nil with 'CFrame'

i was trying to make random spawns but sometimes, it gives me an error attempt to index nil with 'CFrame' when i enter the portal.
Here is the code
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild('Humanoid') and not db and not hit.Parent.Parent:IsA("Tool") then
db = true
local spawns = workspace.Spawns
local spawnPoint = math.random(1,13)
local plr = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
local ss = game:GetService("ServerStorage")
plr.InLobby.Value = false
if UIS.TouchEnabled == true then
script.Ability:Clone().Parent = plr.PlayerGui
end
if not plr.Backpack:FindFirstChild(plr.leaderstats.Glove.Value) and plr.leaderstats.Glove.Value ~= "edgelord" or plr.leaderstats.Glove.Value ~= "ḇ̵̰̪̙̭̎̓o̴̰͈͊̈̓̓̕b̶̨̀͐͌͑͒" then
if ss:FindFirstChild(plr.leaderstats.Glove.Value) then
ss:FindFirstChild(plr.leaderstats.Glove.Value):Clone().Parent = plr.Backpack
hit.Parent.HumanoidRootPart.CFrame = spawns:FindFirstChild(spawnPoint).CFrame
wait(2)
db = false
end
end
end
end)
The "attempt to index nil" error is always telling you that the object you're trying to get a value out of doesn't exist.
In this case, it's probably spawns:FindFirstChild(spawnPoint) that doesn't exist. The FindFirstChild searches the names of the children, and you are passing in a number between 1 - 13. This makes an assumption about the names of the children that is unnecessary and is likely the source of your error.
A safer way to access these objects is to simply get all of the spawn points as an array, and then access a random index within that array.
local spawns = workspace.Spawns:GetChildren()
local randomIndex = math.random(1, #spawns)
local targetCFrame = spawns[randomIndex].CFrame
hit.Parent.HumanoidRootPart.CFrame = targetCFrame

this question is similar to the another, but im trying to make it so when you click a part, it deletes it

while true do
wait(1)
local randomnumber = math.random(18,150)
local x = math.random(1,50)
local Pointgiver = game.ReplicatedStorage.Points:Clone()
Pointgiver.Parent = game.Workspace
Pointgiver.Position = Vector3.new(randomnumber,0.5,x)
Pointgiver.Transparency = 0
end
local pointgiver = game.Workspace:WaitForChild("Points")
pointgiver.ClickDetector.Mousetouch:Connect(function()
pointgiver:Destroy()
end)
as you see, im using a waitforchild function which waits for a part to spawn and doesnt give errors thats like "There is no part named "Points" "
but it wont destroy it, sorry if that didnt make sense
ClickDetector has no function named Mousetouch, instead, you should be using MouseClick which fires whenever the player clicks the respective Part. Also your code which listens for the ClickDetector.MouseClick is after the while loop and as such, it will never run as you keep calling the while loop repeatedly, and never break out of it.
while true do
task.wait(1)
local randomnumber = math.random(18,150)
local x = math.random(1,50)
local Pointgiver = game.ReplicatedStorage.Points:Clone()
Pointgiver.Position = Vector3.new(randomnumber,0.5,x)
Pointgiver.Transparency = 0
PointGiver.ClickDetector.MouseClick:Connect(function()
PointGiver:Destroy()
end)
Pointgiver.Parent = game.Workspace
end

Pathfinder is making my NPC follow my oldest position only

I am trying to make a maze/horror game. I used an online template in the Roblox library as my enemy. I used pathfinder as you will see in the code below. It's finding me like it's supposed to, except it only goes for my LAST position. As you can see in the image below, it completely skipped me, went to my LAST position, then started chasing me. I don't know why it only goes for my last position, and not my current position.
local PathFindingS = game:GetService("PathfindingService")
local humanoid = script.Parent:WaitForChild("Humanoid")
local rootPart = script.Parent:WaitForChild("HumanoidRootPart")
local Players = game:GetService("Players")
game.Workspace.Fruity.Humanoid.WalkSpeed = 60
--To calculate the path
while wait() do
for i, player in pairs(game.Players:GetPlayers()) do
local character = game.Workspace:WaitForChild(player.Name)
local characterPos = character.PrimaryPart.Position
local path = PathFindingS:CreatePath()
path:ComputeAsync(rootPart.Position, characterPos)
game.Workspace.Fruity.HumanoidRootPart:SetNetworkOwner(nil)
local waypoints = path:GetWaypoints()
for i, waypoint in pairs(waypoints) do
local part = Instance.new("Part")
part.Shape = "Ball"
part.Material = "Neon"
part.Size = Vector3.new(0.6,0.6,0.6)
part.Position = waypoint.Position + Vector3.new(0,2,0)
part.Anchored = true
part.CanCollide = false
part.Parent = game.Workspace
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
end
end
Your NPC's pathfinding updates when you call path:ComputeAsync(rootPart.Position, characterPos). The reason it is not updating more frequently is that you are blocking the start of the next loop with the last line : humanoid.MoveToFinished:Wait()
Your code is telling the NPC that it must walk to every single point between every single player, which could take minutes at a time, before ever calculating the path again.
The way to fix this is to make it so that the path can be recalculated quickly and asynchronously. To do this, try something like this :
local PathfindingService = game:GetService("PathfindingService")
local PlayerService = game:GetService("Players")
local humanoid = script.Parent:WaitForChild("Humanoid")
local rootPart = script.Parent:WaitForChild("HumanoidRootPart")
-- create a place where pathfinding orbs can be created and destroyed quickly
local shouldDebugPath = true
local OrbFolder = Instance.new("Folder", game.Workspace)
OrbFolder.Name = "Debug - Orbs"
local OrbTemplate = Instance.new("Part")
OrbTemplate.Shape = Enum.PartType.Ball
OrbTemplate.Material = Enum.Material.Neon
OrbTemplate.Size = Vector3.new(0.6,0.6,0.6)
OrbTemplate.Anchored = true
OrbTemplate.CanCollide = false
local function createWaypoint(position)
local orb = OrbTemplate:Clone()
orb.Position = position + Vector3.new(0,2,0)
orb.Parent = OrbFolder
return orb
end
-- set up some navigation variables and set up a chain
-- so that when the humanoid arrives at a waypoint,
-- it automatically selects the next point.
local currentOrbIndex = 0
local currentPath = {} -- store pathfinding waypoints here
local function moveToNextWaypoint(previousPointReached)
currentOrbIndex += 1
if currentOrbIndex > #currentPath then
return
end
local nextWaypoint = currentPath[currentOrbIndex]
humanoid:MoveTo(nextWaypoint.Position)
end
humanoid.MoveToFinished:Connect(moveToNextWaypoint)
-- calculate and continually update the path
local pauseLength = 1.0 --seconds (lower this number to speed up refresh rate)
while wait(pauseLength) do
-- find the closest player's character model
local targetCharacter = nil
local closestDist = math.huge
local playerList = PlayerService:GetPlayers()
for i, player in pairs(playerList) do
-- check that the player's character has spawned in
local character = player.Character
if not character then
continue
end
-- do some quick maths and select which player is closest
local p1 = character.PrimaryPart.Position
local p2 = rootPart.Position
local d = math.abs((p1 - p2).Magnitude)
if d < closestDist then
closestDist = d
targetCharacter = character
end
end
-- check that there are actually players in the server
if targetCharacter == nil then
continue
end
-- compute the path to the character
local characterPos = targetCharacter.PrimaryPart.Position
local path = PathfindingService:CreatePath()
path:ComputeAsync(rootPart.Position, characterPos)
local waypoints = path:GetWaypoints()
-- debug : show the path to the player
if shouldDebugPath then
OrbFolder:ClearAllChildren()
for i, waypoint in pairs(waypoints) do
local position = waypoint.Position
createWaypoint(position)
end
end
-- start the player walking towards the nearest player
currentOrbIndex = 0
currentPath = waypoints
moveToNextWaypoint()
end
This solution recalculates the path to the nearest player every 1 second. That path is stored as a series of waypoints and it uses the Humanoid.MoveToFinished signal as the trigger to move to the next known waypoint. Whenever the path is recalculated, we simply start the process over.
With this system, the loop isn't blocked and the distance and path to the different players can be regularly recalculated. This allows the NPC to dynamically switch targets towards the closest player too.

What does "attempt to index nil with 'WaitForChild'" mean?

script.Parent.MouseButton1Click:connect(function()
local RS = game:GetService("ReplicatedStorage")
local item = RS:WaitForChild("Pencil")
local price = 350
local player = game.Players.LocalPlayer
local stats = player:WaitForChild("leaderstats")
if stats.Strength.Value>=price then
stats.Strength.Value = stats.Strength.Value - price
local cloned = item:Clone()
cloned.Parent = player.Backpack
cloned.Parent = player.StarterGear
end
end)
I am trying to make a shop and it comes up with "attempt to index nil with 'WaitForChild'" on line 6:
local stats = player:WaitForChild("leaderstats")
I copied it exactly how the video had it and the video had no problem and apparently player has no value even though we set it up just one line above
It means that you are indexing a nil value, which means that player value is nil, which means that game.Players.LocalPlayer on the previous like returns nil. Why that is you need to figure out, as there is not much to go by.
The example shows that it should be local player = game:GetService("Players").LocalPlayer, so you may want to try that.

How do I procedurally iterate through similar variables in Lua?

I have a lot of variables that seem to lend themselves to be referenced a lot easier, but I'm not sure how to do it; the variable names are related to entries on a coordinate system, and where they are written to is also coordinate based. I have tried searching google, I've gone through the Lua documentation, but as I'm not sure what I'm looking for I think that is hindering my search. Here is how the variables look in an excerpt of my code:
-- Bank 1
local Object111 = sItem.getTargetDetails("-4,3,-4")
local Object112 = sItem.getTargetDetails("-5,3,-4")
local Object113 = sItem.getTargetDetails("-6,3,-4")
local Object114 = sItem.getTargetDetails("-7,3,-4")
local Object115 = sItem.getTargetDetails("-8,3,-4")
local Object121 = sItem.getTargetDetails("-4,4,-4")
local Object122 = sItem.getTargetDetails("-5,4,-4")
local Object123 = sItem.getTargetDetails("-6,4,-4")
local Object124 = sItem.getTargetDetails("-7,4,-4")
local Object125 = sItem.getTargetDetails("-8,4,-4")
local Object131 = sItem.getTargetDetails("-4,5,-4")
local Object132 = sItem.getTargetDetails("-5,5,-4")
local Object133 = sItem.getTargetDetails("-6,5,-4")
local Object134 = sItem.getTargetDetails("-7,5,-4")
local Object135 = sItem.getTargetDetails("-8,5,-4")
-- Bank 2
local Object211 = sItem.getTargetDetails("-4,3,1")
local Object212 = sItem.getTargetDetails("-5,3,1")
local Object213 = sItem.getTargetDetails("-6,3,1")
local Object214 = sItem.getTargetDetails("-7,3,1")
local Object215 = sItem.getTargetDetails("-8,3,1")
local Object221 = sItem.getTargetDetails("-4,4,1")
local Object222 = sItem.getTargetDetails("-5,4,1")
local Object223 = sItem.getTargetDetails("-6,4,1")
local Object224 = sItem.getTargetDetails("-7,4,1")
local Object225 = sItem.getTargetDetails("-8,4,1")
local Object231 = sItem.getTargetDetails("-4,5,1")
local Object232 = sItem.getTargetDetails("-5,5,1")
local Object233 = sItem.getTargetDetails("-6,5,1")
local Object234 = sItem.getTargetDetails("-7,5,1")
local Object235 = sItem.getTargetDetails("-8,5,1")
I would then proceed to call each of these variables in a function, where the position on screen is a function of the numbers in the name, the first two numbers relating to x coords and the last number to the y coords:
mon.setCursorPos(4,4)
mon.write(Object111.StoredPercentage)
mon.setCursorPos(10,4)
mon.write(Object112.StoredPercentage)
mon.setCursorPos(16,4)
mon.write(Object113.StoredPercentage)
...
mon.setCursorPos(8,4)
mon.write(Object121.StoredPercentage)
mon.setCursorPos(8,4)
mon.write(Object121.StoredPercentage)
...
mon.setCursorPos(36,4)
mon.write(Object211.StoredPercentage)
mon.setCursorPos(42,4)
mon.write(Object212.StoredPercentage)
mon.setCursorPos(48,4)
mon.write(Object213.StoredPercentage)
etc....
I can see that this should be able to be generated on the fly, but I don't know where to start without calling it out manually; I would prefer it if my code was cleaner. At this stage I really just need to be taught how to fish; if anyone can point me to documents that explain how to do what I'm trying to I would be most grateful.
Here's my final solution for anyone trying to achieve same:
local Banks = 2
local Rows = 3
local Columns = 5
function Objectstatus(bank,row,column)
Id = bank .. row .. column
worldx = "-" .. column+3
worldy = row+2
if bank == 1 then worldz = -4; end
if bank == 2 then worldz = 1; end
Object = {}
Object[Id] = s.getTargetDetails(worldx..","..worldy..","..worldz)
xcursor = (column*7)+3
if bank == 1 then
ycursor = (row*4)+8
end
if bank == 2 then
ycursor = (row*4)+24
end
mon.setCursorPos(xcursor,ycursor)
powertext(Object[Id].StoredPercentage) -- A function that sets Texts settings based on result
mon.write(Object[Id].StoredPercentage) -- Actually writes the result
end
for BankTemp = 1, Banks do
for ColumnTemp = 1, Columns do
for RowTemp = 1, Rows do
Objectstatus(BankTemp,RowTemp,ColumnTemp)
end
end
end

Resources