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

Why does the Regen function not call after the first loop?

I'm trying to find a way to regenerate a tycoon after the previous owner leaves by checking the owner's name (stored in a StringValue) against the playerlist. The first use of the function works perfectly, resetting the tycoon and clearing the StringValue but on the second loop the Regen function is not called.

Any reasons why this is happening and how to fix??

wait(1)
local model = game.ReplicatedStorage:FindFirstChild("Tycoon1")
local floor = script.Parent
local backup = model:Clone()
backup.PrimaryPart = backup.Baseplate
wait(0.1)
backup:SetPrimaryPartCFrame(CFrame.new(floor.Position))
backup.Parent = game.Workspace

local PlayerName = backup.Main.Owner -- StringValue location

local function Regen()
	print("Regeneration Called") -- Does not call on second iteration
    backup:Destroy()
	wait(0.1)
	backup = model:Clone()
	backup.Parent = game.Workspace
	backup.PrimaryPart = backup:FindFirstChild("Baseplate")
	backup:SetPrimaryPartCFrame(CFrame.new(floor.Position))
end

game.Players.PlayerRemoving:Connect(function(Player)
	print(Player.Name .. " has left the game.")
    if Player.Name == PlayerName.Value then
        Regen()
    end
end)

4 comments

0

Check my comment under your other question

0

No problem! Glad I could help!

0

Hey I am working on an answer to your question that was removed. I will send you a link to the codeshare here in a bit

0

never mind you posted the question again in a better way. No need for a codeshare.

1 answer

✓ Accepted answer

I believe the problem is that when you regenerate the tycoon you are failing to set the variable PlayerName to the new owners name. Therefore the if statement on line 24 will not pass. I would recommend checking your other scripts that assign the value to backup.Main.Owner to see if they are the problem.

1 comment

0

I've fixed it! I assumed that the script would redirect to the new 'backup.Main.Owner' when the tycoon was removed but I was obviously wrong. As a part of the regen function I reassigned it and it worked! Thank you!

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