What is the issue? Include screenshots / videos if possible!
I am stuck at the ‘Code Challenge’ at the end, which is to make enemies respawn after they have been defeated. I can’t work out how to get remote event enemySpawnEvent to be triggered within the onDestroyEnemyFunction.
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I’ve tried amending the code provided in the script ‘EnemyHandler’ as suggested (I also picked up onenemySpawnEvent needed to be parented to game.ReplicatedStorage). I searched the forum and web, but there is very little to be said about this particular tutorial. A ‘finished’ copy of the project is available from Roblox, but it does not incorporate the code challenge to make the game endless (ie respawning enemies).
Has anyone been able, or think they will be able, to crack this challenge please?
I feel like I must have missed out on something incredibly simple.
I gave up trying to make this work as per the hidden coding challenge part of the tutorial on the Player Stats page.
I suspect the reason for my failure, is I have not been able to work out how to have the onDestroyEnemy function in BlasterHandler script cause the remote event, ‘EnemySpawnEvent’ to fire spawnEnemies(1) in the EnemyHandler script.
Summary
I did manage to hack a work around.
In EnemyHandler script I added the following variables at the top:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local destroyEnemy = ReplicatedStorage:WaitForChild("DestroyEnemy")
with under the other functions near the bottom of the EnemyHandler script:
spawnEnemies(1)
end
destroyEnemy.OnServerEvent:Connect(onDestroyEnemy)
Please can someone explain what I need to do to have a function in one script to activate a remote event in another?
I really want to be able to complete the challenge in the way the tutorial is trying to show me so I can cement my understanding.
Since I believe the BlasterHandler onDestroyEnemy function is a server script and as well as the enemy handler script you can just use a module script to transfer the function spawn enemy function into the blaster handler script.
Edit: Or you can trigger a custom event for when the enemy dies in the blaster handler script in the form of a BindableEvent.
Or you can even just copy and paste the spawn enemy script function into the destroy enemy function
Full script
-- This script handles blaster projectiles on the server-side of the game
--Skipped the stuff that was already here
--then just add the server sided enemy spawn script in this script
--You might want to transfer the variables as well
--Or make the script modular using ModuleScripts
local function spawnEnemies(count)
local baseplateSize = workspace.Arena.Baseplate.Size
-- Loops until there are the amount of enemies set in ENEMY_COUNT
for i = 1, count do
-- Makes a copy of EnemyBall
local enemy = ServerStorage.Enemies.EnemyBall:Clone()
enemy.Parent = workspace.Enemies
-- Places EnemyBall randomly on the baseplate
local randomX = math.random(baseplateSize.X / -2 - SAFE_ZONE_SIZE, baseplateSize.X / 2 + SAFE_ZONE_SIZE)
local randomZ = math.random(baseplateSize.Z / -2 - SAFE_ZONE_SIZE, baseplateSize.Z / 2 + SAFE_ZONE_SIZE)
enemy.Position = Vector3.new(randomX , 9, randomZ)
-- Assigns a random velocity to EnemyBall
local randomVX = math.random(-1 * MAX_ENEMY_SPEED, MAX_ENEMY_SPEED)
local randomVZ = math.random(-1 * MAX_ENEMY_SPEED, MAX_ENEMY_SPEED)
enemy.Velocity = Vector3.new(randomVX, 0, randomVZ)
enemy.Touched:Connect(onTouched)
end
end
-- Called when the client detects an enemy should be destroyed
local function onDestroyEnemy(player, enemy)
enemy:Destroy()
-- Give the player a point for destroying an enemy
local enemyStat = player.leaderstats:WaitForChild("Score")
enemyStat.Value = enemyStat.Value + 1
--Then spawn 1 enemy to replace it when the enemy is destroyed
spawnEnemies(1)
end
-- Called when a player joins the game. This function sets up a loop that
-- measures the ping of the player
local function onPlayerAdded(player)
while player and wait(2) do
local start = tick()
pingChecker:InvokeClient(player)
local ping = tick() - start
currentPing[player] = ping
end
end
-- Called when a player leaves the game. Removes their entry from the
-- ping table
local function onPlayerRemoving(player)
currentPing[player] = nil
end
-- Set up event bindings
destroyProjectile.OnServerEvent:Connect(onDestroyProjectile)
destroyEnemy.OnServerEvent:Connect(onDestroyEnemy)
Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerRemoving)