I’m new to roblox development and am currently designing a round-based game. I feel that with my current knowledge I can execute this goal but I’d like to complete it with efficient, secure code that utilises remotes. I am still working towards fully understanding client-server interaction and remotes but I mostly have the basics down.
-
What do you want to achieve?
I have a part that, when touched, should fire a remote on the server that removes a player from a table stored on the server. This is replicating how I foresee a player being eliminated from the round i.e. they are struck by another player and removed from the table containing all players still in the round. -
What is the issue?
From some searching, it appears that I can’t use a touched event in a local script. With that said, I can’t fire the server from a server script. How should the client and server be communicating here? Is this even a correct way of designing a round-based game? Anything able to help me understand how to get around this or an explanation of how my approach is wrong would be greatly appreciated.
This is the script that is the child of a part in the workspace:
local part = script.Parent
local function partTouched(otherPart)
print("touched")
local humanoid = otherPart.Parent:FindFirstChildWhichIsA("Humanoid")
if humanoid then
game.ReplicatedStorage.playerEliminated:FireServer(otherPart.Parent)
end
end
part.Touched:Connect(partTouched)
Here is the script in ServerScriptService that handles rounds:
local Players = game:GetService("Players")
local playerTable = game.Players:GetChildren()
local function updatedPlayers()
playerTable = game.Players:GetChildren()
return playerTable
end
while true do
for i = 10, 0, -1 do
print("Intermission: "..i)
wait(1)
end
for _, player in pairs(updatedPlayers()) do
local character = player.Character
character:FindFirstChild("HumanoidRootPart").CFrame = game.Workspace.mapSpawn.CFrame + Vector3.new(0,10,0)
player.RespawnLocation = game.Workspace.lobbySpawn
end
for i = 10, 0, -1 do
print("Round in progress: "..i)
wait(1)
end
updatedPlayers()
for _, player in pairs(playerTable) do
player:LoadCharacter()
end
end
Players.PlayerRemoving:Connect(function(player)
table.remove(playerTable, player)
end)
game.ReplicatedStorage.playerEliminated.OnServerEvent:Connect(function(player)
table.remove(playerTable, player)
print(player.." has been eliminated!")
end)