⚠️ URGENT! ⚠️ Code sometimes working and sometimes not - PLEASE HELP!

Sometimes my code is not working! I am building a tycoon game, and trying to make it quickly to profit, but I still want to make it a quality game, and not a junk game full of free models. I have attached the file of the game for you to test, if you could please examine the code and tell me your thoughts, that would be great! My system so far gives the player a plot, and is supposed to move the button up from under the baseplate after it gives the player the plot, and that is not working. I use a “for _, button in ButtonsFolder:GetChildren()” (this is not the exact code that is in the file, just an example) and it can tell me when it gets the WRONG Buttons, but when it gets to right button, it doesnt work. Please help!
tycoon.rbxl (97.7 KB)
I AM NOT ASKING FOR YOU TO REWRITE MY CODE, I AM ASKING FOR YOUR THOUGHTS ON WHY THIS SOMEITMES WORKS AND SOMEITMES DOESNT.

4 Likes

Hello good. Could you explain a little better, let’s see how I can help you?

4 Likes

Could you send the exact script that isn’t working here?
Sending the output would also be really helpful

3 Likes

Hello! So, I am trying to move buttons that are under the plot that is in the file, up to where the are originally. So, in other words, I want to move a button + 0, 10, 0 position. Every other part of the code is working, just not this part!

2 Likes

If you open the file, you will see. Sometimes my scripts function correctly, and other times they dont. Not sure why.

2 Likes

i keep getting these warnings

so i can’t become a billionaire in your tycoon game :pensive:
(what piece of code is causing the error just send the code)

3 Likes

So… now this piece of code is not working all the time… only sometimes??

local Players = game:GetService("Players")
local Plots = game.Workspace.Plots

print("ready")

print("set")

Players.PlayerAdded:Connect(function(plr)
	print(plr.Name.." added")
	for _, plot in Plots:GetChildren() do
		print("Got for loop")		
		if plot:GetAttribute("Owner") ~= plr.UserId then return end
		
		print("found right")
		
		local buttons = plot:WaitForChild("Buttons")
		
		for _, button in buttons:GetChildren() do
			if button:GetAttribute("IdOfItemToUnlock") ~= 1 then return end
			
			button.Position = button.Position + Vector3.new(0, 10, 0)
		end
	end 
end)


2 Likes

i fixed the other things, just sometimes the button doesnt appear on top of the plot? AKA. this code doesnt function?

1 Like

You can’t just give us the game file and expect us to immediately know why this is happening without any further elaboration. What piece of code is breaking?

1 Like

can you put prints like everywhere and see which part of the code specifically doesn’t run?

additionally, put WaitForChild in your Plots variable, the script might just be executing before that folder loads

2 Likes

Ok thank you so much! THIS WORKS!

1 Like

I thought that the issue is to do with if it sometimes sets the “Owner” attribute after the script checks for “Owner” attribute since PlayerAdded is ran at the same time in both scripts, but sometimes one ends up executing faster than the other so you need to make sure that the attribute has been set first in PlotHandler and then check “if plot:GetAttribute(“Owner”) ~= plr.UserId then return end”. You can test this by printing something after line 10, and run the game a few times.
image

Also I noticed it errors when I left, this seemed to fix it for me.

game.Players.PlayerRemoving:Connect(function(Player)
	for _, plot in Plots:GetChildren() do
		if plot:GetAttribute("Owner") == Player.UserId then 
			plot:SetAttribute("Taken", nil)
			plot:SetAttribute("Owner", nil)
		end
	end
end)

1 Like

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