How to fix this script?

I want to have a working RNG Asteroid Giver Once a Player Survives a Map.
This script below does not give asteroids.


I tried asking people on the forum, I have no errors at all so I have no idea what is wrong.

Heres the code below:

game.ReplicatedStorage.Game.OnPlayerSurvived.OnServerEvent:Connect(function(player, argument)
	for _, Child in pairs(game.Workspace.Multiplayer:GetChildren()) do
		if Child:IsA("Model") then
			if game.Workspace.Multiplayer:FindFirstChildOfClass("Model").Settings.Difficulty.Value == 1 then
				player.leaderstats.Asteroids.Value += math.random(1,5)
			elseif game.Workspace.Multiplayer:FindFirstChildOfClass("Model").Settings.Difficulty.Value == 2 then
				player.leaderstats.Asteroids.Value += math.random(3,10)
			elseif game.Workspace.Multiplayer:FindFirstChildOfClass("Model").Settings.Difficulty.Value == 3 then
				player.leaderstats.Asteroids.Value += math.random(6,15)
			elseif game.Workspace.Multiplayer:FindFirstChildOfClass("Model").Settings.Difficulty.Value == 4 then
				player.leaderstats.Asteroids.Value += math.random(15,35)
			elseif game.Workspace.Multiplayer:FindFirstChildOfClass("Model").Settings.Difficulty.Value == 5 then
				player.leaderstats.Asteroids.Value += math.random(25,50)
			elseif game.Workspace.Multiplayer:FindFirstChildOfClass("Model").Settings.Difficulty.Value == 6 then
				player.leaderstats.Asteroids.Value += math.random(35,80)
			elseif game.Workspace.Multiplayer:FindFirstChildOfClass("Model").Settings.Difficulty.Value == 7 then
				player.leaderstats.Asteroids.Value += math.random(75,135)
			elseif game.Workspace.Multiplayer:FindFirstChildOfClass("Model").Settings.Difficulty.Value == 8 then
				player.leaderstats.Asteroids.Value += math.random(150,180)
			elseif game.Workspace.Multiplayer:FindFirstChildOfClass("Model").Settings.Difficulty.Value == 9 then
				player.leaderstats.Asteroids.Value += math.random(200,265)
			elseif game.Workspace.Multiplayer:FindFirstChildOfClass("Model").Settings.Difficulty.Value == 10 then
				player.leaderstats.Asteroids.Value += math.random(250,315)
			elseif game.Workspace.Multiplayer:FindFirstChildOfClass("Model").Settings.Difficulty.Value == 11 then
				player.leaderstats.Asteroids.Value += math.random(335,450)
			elseif game.Workspace.Multiplayer:FindFirstChildOfClass("Model").Settings.Difficulty.Value == 12 then
				player.leaderstats.Asteroids.Value += math.random(475,665)
			elseif game.Workspace.Multiplayer:FindFirstChildOfClass("Model").Settings.Difficulty.Value == 13 then
				player.leaderstats.Asteroids.Value += math.random(675,750)
			end
		else
			warn("Player"..player.."has beaten a map but its not a valid difficulty or any map at all!")
		end
	end
end)
1 Like

If you aren’t getting any errors or the warning "Player"..player.."has beaten a map but its not a valid difficulty or any map at all!" then either:

  1. game.ReplicatedStorage.Game.OnPlayerSurvived.OnServerEvent is not firing

    or

  1. game.Workspace.Multiplayer has no descendants.
2 Likes

That’s a lot of elseif statements you got there, let me fix that for you! :slight_smile:

-- ServerScript.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local OnPlayerSurvived = ReplicatedStorage:WaitForChild("Game"):WaitForChild("OnPlayerSurvived")
local DifficultyRewards = {
[1] = math.random(1,5);
[2] = math.random(3,10); --Add more yourself! :smiley: 
}
local Multiplayer = workspace:WaitForChild("Multiplayer")

OnPlayerSurvived.OnServerEvent:Connect(function(Player)
	for _, Child in pairs(Multiplayer:GetChildren()) do
		if Child:IsA("Model") then
			local Model = Multiplayer:FindFirstChildWhichIsA("Model")
			if Model then
				local Difficulty = Model:WaitForChild("Settings"):WaitForChild("Difficulty")
				if DifficultyRewards[Difficulty.Value] then
					local Asteroids = Player:WaitForChild("leaderstats"):WaitForChild("Asteroids")
					Asteroids += DifficultyRewards[Difficulty.Value]
				else
					warn("[SERVER]: ERROR while trying to find Rewards for Difficulty Value "..Difficulty.Value.."!"
				end
			end
		end
	end
end)

Edit: I do find it strange that you look through all Multiplayer’s children, and then after that using “FindFirst…” of Multiplayer, and have that defined as “Model”.

2 Likes