Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What might be wrong with this code? [ANSWERED]

What might be wrong with the codes below which make the tycoon bug, and reset over and over after a player leaves the game?

When game left:

model = script.Parent.Factory
backup = model:clone() 

function Regen() 
	model:remove() 
	wait(1) 
	model = backup:clone() 
	model.Parent = script.Parent 
	model:makeJoints() 
end 

while true do 
	wait(1.5) 
	if script.Parent:WaitForChild("Factory").OwnerName.Value ~= "" then 
		if not game.Players:FindFirstChild(script.Parent.Factory.OwnerName.Value) then
		Regen() 
		
		end 
	end 
end 

6 comments

0

Is there anything in the output?

0

The code works, I've tested it in a server via studio, when I left, everything regened nicely, but while testing with more players, it bugged once.

0

How did it bug? Bricks tweaked?

0

It regenerated in loops, even when I became an owner of that House, it said previous owners name + mine at the same time, the whole "tycoon" blinked with bought, and not yet bought items.

0

I believe the bug came from the -loop-

0

Probably did, there is no other way of this looping, is there a way to fix this?

1 answer

✓ Accepted answer

Let's take a step back and think here.

What could we do differently besides a loop, to make it more effective?


##First

We need to create a new script in Workspace that will set the owner's name to nothing in the event he leaves. This is simple, so I won't go into to much detail other than the comments I leave.

function FactoryFind(place, name)
	for _,v in pairs(place:GetChildren()) do
		if v.Name == "Factory" then -- Finding the factory
			if v.Factory.OwnerName.Value == name then
				v.Factory.OwnerName.Value = "" -- Setting name to nothing
			end
		else
			FactoryFind(v) -- If we didn't find factory here, go deeper.
		end
	end
end

game.Players.PlayerLeaving:connect(function (oldPlayer) -- Don't have access to wiki to make sure this is spelled/used right.
	FactoryFind(Workspace, oldPlayer.Name)
end) 

##Now, All we need to do is edit your current script to used the changed event. Let's first create the event:

Note: We are going to keep your regen and variables for consistency.

model = script.Parent.Factory
backup = model:clone() 
ownerName = script.Parent.Factory.OwnerName

function Regen() 
    model:remove() 
    wait(1) 
    model = backup:clone() 
    model.Parent = script.Parent 
    model:makeJoints() 
end 

ownerName.Changed:connect(function (onChange)
	if ownerName.Value == "" then
		Regen
	end
end)

##Finally.. ###In theory, the code I have supplied you, and with the ways mentioned, you can figure out how to consider other possible means yourself in the future. Just need to always keep thinking, and remember, try to solve the problem, not just cover it.

####Note: I have done absolutely no testing with this. I am simply assuming the code given will work, as I do not have studio right now to run any tests on.

5 comments

0

Yes, but there isn't just one "Factory" and the first script would change the Owner Value of a different "Factory" wouldn't it?

0

No, because our If statement catches that. The if statement would make sure the Value of the door is in fact the name of the oldPlayers, and then reset it.

0

Right, let's add that to the game.

0

That works perfectly :)

0

Great!

This discussion is preserved as it was. New answers, comments, and votes are closed.