Question about remote events in my script?

I know it’s a lot of code in your face, but I’d appreciate if you could overlook it and tell me if my remote even I added is correct, and if there is any others I need to add to make this script work. This is my main game script in the workspace. Does it need to be a local script to use remote events?

players = {}

game.Players.PlayerAdded:connect(function(p)
	players[#players+1] = p
end)
game.Players.PlayerRemoving:connect(function(p)
	for i,v in pairs(players) do
		if v == p then
			table.remove(players, i)
		end
	end
end)

function cycle()
	repeat wait() until game.Players.NumPlayers >= 2
	print(players[1])
		local p = players[1] -- scream
	print(players[2])
		local sherrif = players[2]
		repeat wait(1) print("waiting",p) until p ~= nil and sherrif ~= nil
		print(p,"scream")
		print(sherrif,"sherrif")
		
		
		local spawns = workspace.Spawns:GetChildren()
local randomr = math.random(1,#spawns)
local gatherPlayers = game.Players:GetChildren()
script.Playing.Value = 0 -- reset players in session count
for i = 1,#gatherPlayers do
	local giveGameGUI = game.ReplicatedStorage.GameGUI:Clone() -- GIVE CHECK GUI
	giveGameGUI.Parent = gatherPlayers[i].PlayerGui
	gatherPlayers[i].Character:MoveTo(spawns[randomr].Position)
end

local createScreamGUI = game.ReplicatedStorage.YouAreScream:Clone()
createScreamGUI.Parent = p.PlayerGui

local createSherrifGUI = game.ReplicatedStorage.YouAreSherrif:Clone()
createSherrifGUI.Parent = sherrif.PlayerGui

			--print("go") 
		    --print(p.Name)
	
end



while true do
cycle()
print(unpack(players))
script.GameOnOff.Value = true
countdown = 90
wait(1)

script.Playing.Changed:connect(function(changed)
	
	if script.Playing.Value <= 1 then 
		script.GameOnOff.Value = false -- game not in play anymore
		print("Game over!")
	end
	
end)
for i = 1,90 do
	
	wait(1)
	script.Timer.Value = countdown - i
	if players[1] == nil then
		--scream
		script.GameOnOff.Value = false
		print("Scream left!")
		break
	elseif players[1].Character.Humanoid.Health <= 0 then
		script.GameOnOff.Value = false
		print("Scream died!")
		break
	end
	
	if players[2] == nil then
		--sherrif
		print("Sherrif left!")
		break
	elseif players[2].Character.Humanoid.Health <= 0 then
		print("Sherrif died, creating new pistol!")
		script.GameOnOff.Value = false
		break
	end
end
script.GameOnOff.Value = false
local p = players[1]
--local sherrif = players[2]
players[#players+1] = p
--players[#players+1] = sherrif
		table.remove(players, 1)
	--	table.remove(players, 2)
	print("end")
			
	

    local ReplicatedStorage = game:GetService("ReplicatedStorage")
     
    local remoteEvent = ReplicatedStorage:WaitForChild("KillAllPlayers")
     
    -- Fire the remote event
    remoteEvent:FireServer()


wait(10)
end

My remote event’s code:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
 
local remoteEvent = ReplicatedStorage:WaitForChild("KillAllPlayers")
 

local function killPlayers()
local gP = game.Players:GetChildren()
for d = 1,#gP do
	gP[d].Character.Humanoid.Health = 0
	wait()
	end
end

remoteEvent.OnServerEvent:Connect(killPlayers)

Remote events need to be used in local and server scripts working together. From what you stated, it seems you are using 2 scripts.

Ideally, the local script is inside StarterPlayerScripts and Scripts are inside ServerScriptService. If you’re asking how to use the remote events then here you go if not, inform me.

For more reference.

Edit: Forgot to include,

FireServer() -- inside local script
OnServerEvent -- inside script

As daslick said, RemoteEvents are specifically for client-server and server-client communication. If this code is supposed to be server-sided only, you’ll want to look into using BindableEvents and BindableFunctions.

Additionally: please please please be careful when using Remotes! It appears as though your Script involves a RemoteEvent that, when called from any client, would have the server kill all players. This is a huge vulnerability and could be easily abused if someone were to attempt to exploit your game. I’d recommend reading this devforum post on exploiting, as well as doing your own research into how to prevent exploits. A common phrase you’ll hear is “never trust the client:” which is arguably the best preventative measure a developer can take against exploiting.

Happy dev’ing!

So I want to reset the players via killing them at the end of each round.

So I assume on my server script I check if the game is over, and then send a remote event to the client which kills the local players.

There’s really no need to send a message to the client just to kill the player. You can easily just kill the player on the server

You can kill the players just by using the Server. Incorporating the client here would just be over-complicating things. In your main Server script, you could have something along the lines of . . .

-- Here's your new KillAllPlayers() method
local Players = game:GetService("Players")
function KillAllPlayers()
    for _,Player in pairs(Players:GetChildren()) do
        local Character = Player.Character or Player.CharacterAdded:wait()
        if Character:FindFirstChild("Humanoid") then
            Character["Humanoid"].Health = 0
        end
    end
end

-- And then here's a new hypothetical method to end a round,
-- from which you can call the above KillAllPlayers() method
function EndRound()
    -- Whatever you need to do to end the round here
    KillAllPlayers() -- Call the method to kill players.  No remote required!
end

. . . That, and it would work fine, just as an example. Note that the above example for a player-killing code isn’t exactly flawless, but with a few tweaks (specifically with how Characters are found) it would probably suit your needs.

Ok, and my server scripts where should I put them? Or can I just leave it in workspace?

And if you look at my code above, does it need any remote events? Or can I just leave it all in server script?

Typically, your scripts should be stored in ServerScriptService unless there’s a very specific reason why it needs to be stored somewhere else.

Your code doesn’t seem to need any Remotes, except for perhaps the Gui part. For toggling the player’s Gui at the start of the round, I’d recommend:

  1. Storing the Gui objects inside the PlayerGui and toggling visible = false for all components that need a role (Sheriff, etc.)
  2. Creating a RemoteEvent; when the server assigns a role to a player, it fires the RemoteEvent to that player to let them know what role they’ve been assigned.
  3. Add code somewhere on the client that listens to that RemoteEvent. When the RemoteEvent is fired for them, toggle the appropriate Gui to visible = true.

Something along those lines :slight_smile:

1 Like

You can’t fire a remote event to a server from a server. Remote events were made to send data from the client to the server or vice versa. If you need to fire a remote event from the client to the client or from the server to the server, you probably didn’t do your scripts well enough because of multiple reasons:

  1. That data should’ve been available to all over server scripts.

  2. Those 2 scripts can and should probably be merged together if the data isn’t shared with every other server script.

I suggest looking at the both of these and see if you can easily share the data or instead merge the two scripts.

1 Like

One more question, can a server script not affect a player? Like can I not just manually change a players PlayerGui GUI like if he’s the sheriff or whatever character. Or does a server script not have any affect on stuff like that? (Without remote event to local script)

The server can’t change anything inside the PlayerGyu object. It can change things on the player object and the character object but nothing in the UI.

1 Like