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

This is a big one. I can't find the conflict in these two scripts. Help?

I made a game called Random BattleGrounds of Randomness 2 and I used the Disaster Kit V2 the I've modified as a base for the game as it could do stuff I wanted to do in the game and I figured it'd make it a bit easier. Well, now I'm looking to update the game with a K/D leaderboard but when I do it breaks the disaster kit. Normally, in the game, it has a 90 second timer between disasters (Which are labeled Hazards in my game), when the timer reaches 0 a disaster plays, afterwards the timer counts back down again. However, when I try to use a Leaderboard script along with it it just breaks and I can't figure out why, I assume there's a conflict somewhere. I've tried everything I could think of, but being pretty novice when it comes to Lua I've run out of ideas.

Here's the Leaderboard script:

local Players = game.Players

local Template = Instance.new 'BoolValue'
Template.Name = 'leaderstats'

Instance.new('IntValue', Template).Name = "Kills"
Instance.new('IntValue', Template).Name = "Deaths"


Players.PlayerAdded:connect(function(Player)
	wait(1)
	local Stats = Template:Clone()
	Stats.Parent = Player
	local Deaths = Stats.Deaths
	Player.CharacterAdded:connect(function(Character)
		Deaths.Value = Deaths.Value + 1
		local Humanoid = Character:FindFirstChild "Humanoid"
		if Humanoid then
			Humanoid.Died:connect(function()
				for i, Child in pairs(Humanoid:GetChildren()) do
					if Child:IsA('ObjectValue') and Child.Value and Child.Value:IsA('Player') then
						local Killer = Child.Value
						if Killer:FindFirstChild 'leaderstats' and Killer.leaderstats:FindFirstChild "Kills" then
							local Kills = Killer.leaderstats.Kills
							Kills.Value = Kills.Value + 1
						end
						return 
					end
				end
			end)
		end
	end)
end)

And here's the game script:

local dataStore = require(game.ServerStorage.DataStore)
local survivors = {}
local currentDisaster = nil
local timeToCount = 0
local rounds = 0
local states = Instance.new("Folder")
states.Name = "PlayerStates"

local lighting = game:GetService("Lighting")

function regenStructures()
	workspace.Structures:ClearAllChildren()
	local maps = game.ServerStorage.Clones.Maps:GetChildren()
	local mapCopy = maps[math.random(1, #maps)]:Clone()
	mapCopy.Parent = workspace.Structures
	mapCopy:MakeJoints()
end

function compilePlayers()
	local names = ""
	if #survivors == 1 then return survivors[1] end
	for i = 1, #survivors do
		if i == #survivors then
			names = names.. "and ".. survivors[i]
		else
			names = names.. survivors[i] .. ", "
		end
	end
	return names
end

function died(playerName, humanoid)
	if humanoid and humanoid.Health == 0 then
		for i, v in pairs(survivors) do
			if v == playerName then
				table.remove(survivors, i)
			end
		end
	end	
end

function destroyMessage()
	for i, v in pairs(workspace:GetChildren()) do
		if v:IsA("Message") then
			v:Destroy()
		end
	end
end

function createOrUpdateMessage(msg)
	if workspace:FindFirstChild("_Message") then
		workspace["_Message"].Text = msg
	else
		local message = Instance.new("Message")
		message.Name = "_Message"
		message.Text = msg
		message.Parent = game.Workspace
	end
end

function destroyHint()
	for i, v in pairs(lighting:GetChildren()) do
		if v:IsA("StringValue") then
			v.Value = ""
		end
	end
end

function createOrUpdateHint(msg)
	if lighting:FindFirstChild("_Hint") then
		lighting["_Hint"].Value = msg
	else
		local message = Instance.new("StringValue")
		message.Name = "_Hint"
		message.Value = msg
		message.Parent = game.Lighting
	end
end

function countDown(cdt, msg)
	for i = 1, cdt do
		createOrUpdateHint(msg .. cdt - i + 1)
		wait(1)
	end
end

function _G.updateLeaderboard()
	for i, v in pairs(game.Players:GetChildren()) do
		local leaderstats = v:FindFirstChild("leaderstats")
		if leaderstats then
			local fakePoints = leaderstats:FindFirstChild("Points")
			local fakeSurvivals = leaderstats:FindFirstChild("Survivals")
			fakePoints.Value = dataStore:GetStat(v, "Points") or 0
			fakeSurvivals.Value = dataStore:GetStat(v, "Survivals") or 0
		end
	end
end

regenStructures()
while true do
	if rounds == roundsToRegenStructures then
		regenStructures()
		rounds = 0
	end 
	rounds = rounds + 1
	countDown(breakTime, "Oh no! A Hazard is coming in ")
	_G.updateLeaderboard()
	destroyHint()
	local connections = {}
	survivors = {}
	for i, v in pairs(game.Players:GetChildren()) do
		if v.Character and v.Character:FindFirstChild("Humanoid") then
			table.insert(survivors, v.Name)
			table.insert(connections, 
				v.Character.Humanoid.HealthChanged:connect(function() 
					died(v.Name, v.Character.Humanoid) 
				end)
			)
		end
	end
	currentDisaster = disasters[math.random(#disasters)]
	if game.ServerStorage.Disasters:FindFirstChild(currentDisaster[1]) then -- disaster exists, proceed to spawn it
		--
		local disasaterCopy = game.ServerStorage.Disasters[currentDisaster[1]]:clone()
		disasaterCopy.Parent = game.Workspace
		disasaterCopy:makeJoints()
		countDown(currentDisaster.Duration, "Watch Out! Active Hazard " .. currentDisaster[1] .. " will be leaving in ")
		destroyHint()
		disasaterCopy:Destroy()
		--
		for i, v in pairs(survivors) do
			local player = game.Players:FindFirstChild(v)
			if player then
				dataStore:AddStat(player, "Points", currentDisaster.Points) -- award survivors points
				dataStore:AddStat(player, "Survivals", 0) -- award survivors 1 survival
			end
		end
		_G.updateLeaderboard()
		wait(3)
		destroyMessage()
	else
		-- warn dev
		createOrUpdateHint('ERROR: The game has failed to locate "' .. currentDisaster[1] .. '" in the Disasters folder in ServerStorage.')
		while true do
			wait(1)
		end
	end
end

What it ends up doing is, when the timer gets to 0, it just sits there. No disaster spawns, it doesn't reset the timer, it just does nothing.

Any help would be appreciated

2 comments

0

It would be helpful if you included the issue.

0

I'm sorry, I'm fairly forgetful. The issue is that when the timer gets to 0, it just sits there. No disaster spawns, it doesn't reset the timer, it just does nothing.

1 answer

I haven't read all of your game code but I think on line 44 for the if statement you can try to add in

and Message.Text == 0

Sorry if I am mistaken.

1 comment

0

It gives a syntax error for the "and", causes the game to not function, unfortunately that doesn't seem like the solution. Thank you though

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