Parts arent changing material correctly

I want it so when the player has enough money, the parts material will change to neon

The material changing is inconsistent and broken, most of the time only one button will glow, other times maybe 2 will glow but they will not update based on the players cash

Here is my explorer:

Code:

while wait(0.1) do
	local mult = game.Workspace.Buttons.Base.Multi
	local plr = game.Players.LocalPlayer
	local stat = plr:WaitForChild("Data"):WaitForChild("Stats"):WaitForChild("Money")
	local x = require(game.ReplicatedStorage.EternityNum)

	for _, button in mult.Mul.Glow:GetChildren() do
		local buttons = mult.Mul
		local config = buttons.config
		if x.meeq(x.convert(stat.Value), (x.convert(config.cost.Value))) then
			button.Material = Enum.Material.Neon
		else
			button.Material = Enum.Material.SmoothPlastic
		end
	end

end


1 Like

Let me help you Where did you use the script?

1 Like

Here I made some changes and optimized your code:

local mult = game:GetService(“Workspace”):WaitForChild(“Buttons”):WaitForChild(“Base”):WaitForChild(“Multi”)
local plr = game:GetService(“Players”).LocalPlayer
local stat = plr:WaitForChild(“Data”):WaitForChild(“Stats”):WaitForChild(“Money”)
local x = require(game.ReplicatedStorage.EternityNum)

while wait(0.1) do
local GlowChildren = mult.Mul.Glow:GetChildren()
for _, button in pairs(GlowChildren) do
local buttons = mult.Mul
local config = buttons.config
if x.meeq(x.convert(stat.Value), x.convert(config.cost.Value)) then
button.Material = Enum.Material.Neon
else
button.Material = Enum.Material.SmoothPlastic
end
end
end

Why:

  1. I think base on your script the variable doesn’t need to update at this point so I put it on the top and added some “:WaitForChild(”“)” to wait for its child before proceeding.

  2. here in “for _, button in mult.Mul.Glow:GetChildren() do” i change it into " local GlowChildren = mult.Mul.Glow:GetChildren()
    for _, button in pairs(GlowChildren) do" so the Glow’s children will save in GlowChildren and loop all things inside it. hopes this helps

1 Like

Too much WaitForChild()s, at least cut it after you access the first children of Workspace. You might also seek Changed event as a better alternative.

1 Like

All you did was reword my code, does not solve the original issue

1 Like

How is this even working and not throwing an Attempt to index nil with Glow error? mult doesn’t contain anything named Mul.

Avoid using keywords for variables names.

You are using custom functions that you didn’t share.

1 Like

Forgot to mention name gets changed to mul for another script
image
The if then statement just checks to see if the player has enough money in eternitynum terms

1 Like

The problem is pretty simple, you are iterating just one pad’s children. From my understanding the Multi folder contains all the pads models. If that’s the case then the correct way of iterating is:

for _, pad in pairs(mult:GetChildren()) do
    for _, button in pad.Glow:GetChildren() do
		local buttons = mult.Mul
		local config = buttons.config
		if x.meeq(x.convert(stat.Value), (x.convert(config.cost.Value))) then
			button.Material = Enum.Material.Neon
		else
			button.Material = Enum.Material.SmoothPlastic
		end
	end
end
1 Like

This only makes every glow part glow instead of making only the ones the player can afford glow.

My bad, forgot to change the variable used to get config

for _, pad in pairs(mult:GetChildren()) do
    for _, button in pairs(pad.Glow:GetChildren()) do
		local config = pad.config
		if x.meeq(x.convert(stat.Value), (x.convert(config.cost.Value))) then
			button.Material = Enum.Material.Neon
		else
			button.Material = Enum.Material.SmoothPlastic
		end
	end
end

Anwyay you should revise your variables naming because it’s very confusing.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.