Help defining players in serverscript

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want the easiest way to reference players who activate the triggers for certain events.

  2. What is the issue? Include screenshots / videos if possible!
    As of now i had the code with FireAllClients and then i realized it makes stuff appear for everyone on trigger.

Example: Someone grabs a key, that triggers the event that updates the objectives and such but for everyone, even people without the key.

I want to change it to FireClient so it only appears to whoever procs the triggers, BUT i dont know how to define the player and keep getting errors, im noob at scripting.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Videos, tutorials, some other posts on the forums but im not sure where to put the lines of code and some community discords but no luck to fix the problem as id want to yet.

Also my script is based on this video: ROBLOX Studio Horror Game Tutorial Part 1 - YouTube
My main issue would be that the triggers get destroyed when procc, so if i were to change it to FireClient i think i would need the triggers to stay BUT i use the following line to change the texts and dont know for what i could replace it.

repeat wait() until game.Workspace.Triggers.DialogueTriggers:FindFirstChild("TriggerDialogue1") == nil

As for the full code, its here.
This one is my Main Script, its on ServerScriptService

local DialogueEvent = game.ReplicatedStorage.RemoteEventsTasks:FindFirstChild("DialogueEvent")
local ObjectiveEvent = game.ReplicatedStorage.RemoteEventsTasks:FindFirstChild("ObjectiveEvent")

local ToggleDialogueEvent = game.ReplicatedStorage.RemoteEventsTasks:FindFirstChild("ToggleDialogueEvent")
local ToggleObjectiveEvent = game.ReplicatedStorage.RemoteEventsTasks:FindFirstChild("ToggleObjectiveEvent")

local function MainGame()
	ToggleDialogueEvent:FireAllClients(true) --TOGGLES DIALOGUE TO VISIBLE
	DialogueEvent:FireAllClients("We finally arrived, i should find the rest of the crew") --WHAT DIALOGUE YOU WANT
	wait(8)
	ToggleDialogueEvent:FireAllClients(false) --TOGGLES DIALOGUE TO INVISIBLE
	ToggleObjectiveEvent:FireAllClients(true) --TOGGLES OBJECTIVE TO VISIBLE
	ObjectiveEvent:FireAllClients("Find the Key for the Church") --WHAT OBJECTIVE YOU WANT
	
	repeat wait() until game.Workspace.Triggers.DialogueTriggers:FindFirstChild("TriggerDialogue1") == nil --THE TRIGGER ON TOUCH FOR A NEW DIALOGUE/OBJECTIVE (FINDING LOGAN)
	
	ToggleDialogueEvent:FireAllClients(true) --TOGGLES DIALOGUE TO VISIBLE
	DialogueEvent:FireAllClients("Who is that across the lake? I should get closer and talk with him") --WHAT DIALOGUE YOU WANT
	wait(8)
	ToggleDialogueEvent:FireAllClients(false) --TOGGLES DIALOGUE TO INVISIBLE
	
	repeat wait() until game.Workspace.Triggers.ObjectiveTriggers.ChurchKeySpawner:FindFirstChild("Hitbox") == nil --THE TRIGER ON TOUCH FOR A NEW DIALOGUE/OBJECTIVE (FINDING CHURCH KEY)
	
	ToggleDialogueEvent:FireAllClients(true) --TOGGLES DIALOGUE TO VISIBLE
	DialogueEvent:FireAllClients("Found the Key, now to the Church and see what it opens") --WHAT DIALOGUE YOU WANT
	wait(8)
	ToggleDialogueEvent:FireAllClients(false) --TOGGLES DIALOGUE TO INVISIBLE
	ToggleObjectiveEvent:FireAllClients(true) --TOGGLES OBJECTIVE TO VISIBLE
	ObjectiveEvent:FireAllClients("Use the Key inside the Church") --WHAT OBJECTIVE YOU WANT
	
	repeat wait() until game.Workspace.Triggers.ObjectiveTriggers:FindFirstChild("ArriveCrematorium") == nil --THE TRIGER ON TOUCH FOR A NEW DIALOGUE/OBJECTIVE (REACH CREMATORIUM)
	
	ToggleDialogueEvent:FireAllClients(true) --TOGGLES DIALOGUE TO VISIBLE
	DialogueEvent:FireAllClients("The Church trapdoor was finally opened") --WHAT DIALOGUE YOU WANT
	wait(8)
	ToggleDialogueEvent:FireAllClients(false) --TOGGLES DIALOGUE TO INVISIBLE
	ToggleObjectiveEvent:FireAllClients(true) --TOGGLES OBJECTIVE TO VISIBLE
	ObjectiveEvent:FireAllClients("Find a way out of the Crematorium") --WHAT OBJECTIVE YOU WANT
	
		end
		

wait(25)
MainGame()

And my triggers script is this

script.Parent.Touched:Connect(function(hit)
	if hit and hit.Parent:FindFirstChild("Humanoid") then
		script.Parent:Destroy()
	end
end)

Would greatly appreciate if someone could help out, thanks for the read

remote events auto have a player as a parameter, you also can tamper with player guis in serverscripts instead of using remote events and causing network traffic and you can test who hits it in the touched function

game.Players:GetPlayerFromCharacter(hit.Parent)

maybe send a bindable event instead of destroy()

what do you mean by bindable event, im looking it up but dont quite understand how to switch it up. But yea i agree something instead of destroy would be good so i can change in the MainScript the parts that say

repeat wait() until game.Workspace.Triggers.DialogueTriggers:FindFirstChild("TriggerDialogue1") == nil --THE TRIGGER ON TOUCH FOR A NEW DIALOGUE/OBJECTIVE (FINDING LOGAN)

you can use a bindable event, its like a remote event but for script to script or local to local

bindable.Event:Connect(function(params)

end)
bindable:Fire(params)

you can have one main script for dialogue and stuff for the .Event then all your smaller scripts fire to that bindable with information like what its meant to be doing and who its meant to be doing it to.

The few ways to define players in the server would be using a for loop to get all of the players inside of game.Players, but even if you have the player indexed you won’t be able to access all parts of the player (eg: PlayerScripts is unable to be accessed from the server). You can also try to make a table with all of the players userids. Another way to get the player from the server would be via remotes. An example is shown below.

local players = {};

game.Players.PlayerAdded:Connect(function(plr)
table.insert(players, plr.UserId);
end)

game.Players.PlayerRemoving:Connect(function(plr)
table.remove(players, plr.UserId);
end)
-- remotes
local rem = game.ReplicatedStorage.ARandomRemote

rem.OnServerInvoke:Connect(function(plr)
return true;
-- whenever InvokeServer or FireServer is used, the first thing in the "function()" is automatically the player, the server gets this so there's no need to index the player in the line of code which fires these events.
end)
1 Like

Thank you for the in deph reply, since im not very confident in scripting and also english is not my main language ill ask. The first script you typed
EDIT: I have to use both scripts right? or just one

local players = {};

game.Players.PlayerAdded:Connect(function(plr)
table.insert(players, plr.UserId);
end)

game.Players.PlayerRemoving:Connect(function(plr)
table.remove(players, plr.UserId);
end)

would be in a localscript in startercharacter right?

and then the second script

-- remotes
local rem = game.ReplicatedStorage.ARandomRemote

rem.OnServerInvoke:Connect(function(plr)
return true;
-- whenever InvokeServer or FireServer is used, the first thing in the "function()" is automatically the player, the server gets this so there's no need to index the player in the line of code which fires these events.
end)

apart from creating a RemoteEvent in ServerStorage, the second script would be applied in my main script before the main function like this?

local DialogueEvent = game.ReplicatedStorage.RemoteEventsTasks:FindFirstChild("DialogueEvent")
local ObjectiveEvent = game.ReplicatedStorage.RemoteEventsTasks:FindFirstChild("ObjectiveEvent")

local ToggleDialogueEvent = game.ReplicatedStorage.RemoteEventsTasks:FindFirstChild("ToggleDialogueEvent")
local ToggleObjectiveEvent = game.ReplicatedStorage.RemoteEventsTasks:FindFirstChild("ToggleObjectiveEvent")

local rem = game.ReplicatedStorage.ARandomRemote

rem.OnServerInvoke:Connect(function(plr)
return true;
-- whenever InvokeServer or FireServer is used, the first thing in the "function()" is automatically the player, the server gets this so there's no need to index the player in the line of code which fires these events.
end)

local function MainGame()
	ToggleDialogueEvent:FireAllClients(true) --TOGGLES DIALOGUE TO VISIBLE
	DialogueEvent:FireAllClients("We finally arrived, i should find the rest of the crew") --WHAT DIALOGUE YOU WANT
	wait(8)
	ToggleDialogueEvent:FireAllClients(false) --TOGGLES DIALOGUE TO INVISIBLE
	ToggleObjectiveEvent:FireAllClients(true) --TOGGLES OBJECTIVE TO VISIBLE
	ObjectiveEvent:FireAllClients("Find the Key for the Church") --WHAT OBJECTIVE YOU WANT
	
	repeat wait() until game.Workspace.Triggers.DialogueTriggers:FindFirstChild("TriggerDialogue1") == nil --THE TRIGGER ON TOUCH FOR A NEW DIALOGUE/OBJECTIVE (FINDING LOGAN)
	
	ToggleDialogueEvent:FireAllClients(true) --TOGGLES DIALOGUE TO VISIBLE
	DialogueEvent:FireAllClients("Who is that across the lake? I should get closer and talk with him") --WHAT DIALOGUE YOU WANT
	wait(8)
	ToggleDialogueEvent:FireAllClients(false) --TOGGLES DIALOGUE TO INVISIBLE
	
	repeat wait() until game.Workspace.Triggers.ObjectiveTriggers.ChurchKeySpawner:FindFirstChild("Hitbox") == nil --THE TRIGER ON TOUCH FOR A NEW DIALOGUE/OBJECTIVE (FINDING CHURCH KEY)
	
	ToggleDialogueEvent:FireAllClients(true) --TOGGLES DIALOGUE TO VISIBLE
	DialogueEvent:FireAllClients("Found the Key, now to the Church and see what it opens") --WHAT DIALOGUE YOU WANT
	wait(8)
	ToggleDialogueEvent:FireAllClients(false) --TOGGLES DIALOGUE TO INVISIBLE
	ToggleObjectiveEvent:FireAllClients(true) --TOGGLES OBJECTIVE TO VISIBLE
	ObjectiveEvent:FireAllClients("Use the Key inside the Church") --WHAT OBJECTIVE YOU WANT
	
	repeat wait() until game.Workspace.Triggers.ObjectiveTriggers:FindFirstChild("ArriveCrematorium") == nil --THE TRIGER ON TOUCH FOR A NEW DIALOGUE/OBJECTIVE (REACH CREMATORIUM)
	
	ToggleDialogueEvent:FireAllClients(true) --TOGGLES DIALOGUE TO VISIBLE
	DialogueEvent:FireAllClients("The Church trapdoor was finally opened") --WHAT DIALOGUE YOU WANT
	wait(8)
	ToggleDialogueEvent:FireAllClients(false) --TOGGLES DIALOGUE TO INVISIBLE
	ToggleObjectiveEvent:FireAllClients(true) --TOGGLES OBJECTIVE TO VISIBLE
	ObjectiveEvent:FireAllClients("Find a way out of the Crematorium") --WHAT OBJECTIVE YOU WANT
	
		end
		

wait(25)
MainGame()

how could i call it if i were to change the FireAllClients in some lines for FireClient. Would something like this work?

ToggleDialogueEvent:FireClient(plr, true) --TOGGLES DIALOGUE TO VISIBLE

or

ToggleDialogueEvent:FireClient(true) --TOGGLES DIALOGUE TO VISIBLE

or the other idea i got from what you explained

ToggleDialogueEvent:FireClient(plr(), true) --TOGGLES DIALOGUE TO VISIBLE

if its all wrong its ok, lemme know i wanna understand how to apply it to my code

You’d have to do ToggleDialogueEvent:FireClient(plr, true), only when firing to all clients you don’t have to index a player.