How can I get Character from ServerScript?

  1. What do you want to achieve?
    When the player triggers the ProximityPrompt, it fires the event from the ServerScript to the Client.

  2. What is the issue?
    I get this error:

Workspace.Model.DoorC1.Prox.ProximityPrompt.Script:8: attempt to index nil with 'PlayerGui' 
  1. What solutions have you tried so far?
    I’ve tried using
local player = game.Players.LocalPlayer

but to the best of my knowledge, that only works for LocalScripts, not ServerScripts.

ProximityPrompt ServerScript:

script.Parent.Triggered:Connect(function(prompt)
	script.Sound:Play()
	
	local character = prompt.Parent
	local player = game.Players:GetPlayerFromCharacter(character)
			local playergui = player["PlayerGui"]
			local textbox = playergui.Fade.Transition
			--textbox.Text = text
			print("fade triggered")
			textbox.Fader:FireClient(player)
	
end)

The thing you have named prompt is the actual player instance and character is not a parent of the player.

1 Like

Triggered returns the player instance, so just do prompt.Character

1 Like

You can not access LocalPlayer unless you are using a LocalScript (or ModuleScript accessed by a LocalScript).

Luckily, the Triggered event already passes the player, so you can change your code to this:

script.Parent.Triggered:Connect(function(player)
	script.Sound:Play()
	
	local character = prompt.Parent
	local playergui = player["PlayerGui"]
	local textbox = playergui.Fade.Transition
	--textbox.Text = text
	print("fade triggered")
	textbox.Fader:FireClient(player)
end)

(See ProximityPrompt.Triggered for more information.)

1 Like

Thank you!! This worked perfectly!!! :hearts:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.