Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to Make GUI appear for only a person when killing NPC?

I'm making it so a gui appears when by killing an npc . But when someone kills the npc, it appears on all screens, what would I do.

local InUse = false
NPC.Humanoid.Died:Connect(function()
	wait(4)
	if InUse == false then
		InUse = true
		Camera.CameraType = Enum.CameraType.Scriptable
		if Camera.CameraType == Enum.CameraType.Scriptable and UIS.TouchEnabled == true then
			chatline:TweenPosition(UDim2.new(0, 0,0.71, 0),"Out", "Quint",1.5)
			Camera.CFrame = CamPart3.CFrame
			wait(0.4)

1 comment

0

Dude how did you username not get banned haha

1 answer

This is likely because the event fires on every single client. The Died event should have a listener on the server, not the client. Furthermore, you will need to identify who killed the NPC so you can perform the action for that player only.

For client-server communication, you may use a RemoteEvent. This Instance holds a function called FireClient(), which fires a signal to the given player, and only that player may receive the signal with their script(s). For example, suppose there are 4 players in-game. This would randomly select one of those players and fire a signal to them:

local event = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent")
local players = game:GetService("Players"):GetPlayers()
local plr = players[math.random(1, #players)]
event:FireClient(plr)

If we were to do something in a LocalScript that would receive the signal via OnClientEvent, that player would have its code executed but no other player would receive the signal. For example, this would print fired once because only one player receives the signal:

local event = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent")
event.OnClientEvent:Connect(function()
	print('fired')
end)

If FireAllClients() was used, the amount of times fired would be output would be equal to the number of players in game #players. The same is not always true if FireServer() is fired to the server from the client. One client can fire the event and the code within the receiving function would run once at a time.

This discussion is preserved as it was. New answers, comments, and votes are closed.