Script only works once?

I’m trying to make a leadership system similar to grg and in my leadership system when I playtest it for the first time it works, but when I play test again suddenly the script doesn’t work when I try to reset the progress, however, the buying the tycoon part works? This repeatedly happens even I start a new session of play testing.

— please excuse my long script —

local Teams = game:GetService("Teams")

local Viking_Buttons = workspace:FindFirstChild("VikingButtons")
local Viking_Purchased = workspace:FindFirstChild("VikingPurchased")
local Viking_Objects = {}

local Human_Buttons = workspace:FindFirstChild("HumanButtons")
local Human_Purchased = workspace:FindFirstChild("HumanPurchased")
local Human_Objects = {}


local function setupButtons(teamName, buttons, purchasedObjects, objectsTable)
	if buttons then
		for i, v in pairs(buttons:GetChildren()) do
			spawn(function()
				if v:FindFirstChild("Button") then

					local newObject = purchasedObjects:FindFirstChild(v.Object.Value)
					if newObject ~= nil then
						objectsTable[newObject.Name] = newObject:Clone()
						newObject:Destroy()
					else
						v.Button.Transparency = 1
						v.Button.CanCollide = false
						v.Button.Container.Store.Visible = false
					end

					v.Button.Touched:Connect(function(hit)
						local player = game.Players:GetPlayerFromCharacter(hit.Parent)
						if player then
							if player.Team == Teams[teamName] then
								if v.Button.CanCollide == true and v.Button.Transparency == 0 then
									objectsTable[v.Object.Value].Parent = purchasedObjects
									v.Button.CanCollide = false
									v.Button.Transparency = 1
									v.Button.Container.Store.Visible = false
								end
							end
						end
					end)
				end
			end)
		end
	end
end



local function ResetProgress(buttons, purchasedObjects, objectsTable)
	for i, v in pairs(buttons:GetChildren()) do
		local NewItem = purchasedObjects:FindFirstChild(v.Object.Value)
		v.Button.CanCollide = true
		v.Button.Transparency =  0
		v.Button.Container.Store.Visible = true
		for i, v in pairs(purchasedObjects:GetChildren()) do
			objectsTable[NewItem.Name] = NewItem:Clone()
			v:Destroy()
		end
	end
end


setupButtons("Viking Leader", Viking_Buttons, Viking_Purchased, Viking_Objects)
setupButtons("Human Leader", Human_Buttons, Human_Purchased, Human_Objects)

game:GetService("Players").PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function()
			if player.Team == Teams["Human Leader"] then
				ResetProgress(Human_Buttons, Human_Purchased, Human_Objects)
			end
			if player.Team == Teams["Viking Leader"] then
				ResetProgress(Viking_Buttons, Viking_Purchased, Viking_Objects)
			end
		end)
	end)
end)

The TestFile if you want to try and fix it
TestFile.rbxl (59.9 KB)

I honestly, believe this is a roblox problem but I dont know.

1 Like

That’s odd.

Try the following:

Script Type (server or local)
Place of script - serverscriptservice

If not tell me and I will have a closer look on my pc.

1 Like

That’s what I did it’s a server sided script inside of a serverscriptservice the issue still persists, I don’t know why. I also added a TestFile for the scripts, the error I recieved is that when I play test the game numerous times, the script sometimes doesn’t work and stops working after that even when I attempt a new play test.

1 Like

image

You dont need this. You can just do

game:GetService("Players").PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function()
			if player.Team == Teams["Human Leader"] then
				ResetProgress(Human_Buttons, Human_Purchased, Human_Objects)
			end
			if player.Team == Teams["Viking Leader"] then
				ResetProgress(Viking_Buttons, Viking_Purchased, Viking_Objects)
			end
			
			player.Team = Teams["Spectator"]
		end)
	end)
end)

Ain’t no way, that was the reason why the script didnt work consistently💀

1 Like

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