If close to player activate keybind

I’m making a game about history,.
I want people to be able to talk to npc’s.
But my code isnt working, and it prints no errors.Probably cause of the functions (idk how to use them)
Theres a script that returns it but for right now it just prints “hi” when the event is fired.

local UserInputService = game:GetService("UserInputService")

while wait(.1) do
	UserInputService.InputBegan:Connect(function(InputObject, Processed)
		if game.Players.LocalPlayer:DistanceFromCharacter(script.Parent.Position) <= 10000 then
			if InputObject.KeyCode == Enum.KeyCode.K then
				local players = game.Players.LocalPlayer
				local text = "test thingy mabob."
				game.ReplicatedStorage.talktoevent:FireServer(text,players)
			end
		end
	end)
	end

If the localscript is in the workspace it won’t work.

It’s inside a torso of the desired NPC.

It won’t run then, Try putting a normal script in it, then fire a remote event. have this remote event be picked up by a local script in the player.

I will try that then, thanks give me a moment.

Playerscripts or characterscripts?

You are adding events upon events inside of this script. Only do it once.

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(InputObject, Processed)
	if game.Players.LocalPlayer:DistanceFromCharacter(script.Parent.Position) <= 10000 then
		if InputObject.KeyCode == Enum.KeyCode.K then
			local players = game.Players.LocalPlayer
			local text = "test thingy mabob."
			game.ReplicatedStorage.talktoevent:FireServer(text,players)
		end
	end
end)

Also, localscripts will only work inside of workspace if it is inside a PLAYERS character, not a custom NPC. Add it into the players character. and change it to work properly.

Ill try this. thank you give me a moment.
PS: There will be multiple NPCs. Do I need to make a script inside the player for all the NPCs?

You would need to do that. To shorten it, just add a folder of NPC’s and go through the folder, something like this:

local UserInputService = game:GetService("UserInputService")

local NPCFolder = --the folder

UserInputService.InputBegan:Connect(function(InputObject, Processed)
	for i,v in pairs(NPCFolder:GetChildren()) do
		if game.Players.LocalPlayer:DistanceFromCharacter(v.Torso.Position) <= 10000 then --change it to whatever part of the NPC.
			if InputObject.KeyCode == Enum.KeyCode.K then
				local players = game.Players.LocalPlayer
				local text = "test thingy mabob."
				game.ReplicatedStorage.talktoevent:FireServer(text,players)
			end
		end
	end
end)

They will have different texts tho.

Its all good, except. When it prints what the text should be, instead it prints my username?
and then if I add value, it says value is not a valid member of game.players.stick_man1112

game.ReplicatedStorage.talktoevent.OnServerEvent:Connect(function(EA)
	
	print(EA.Value)
end)

The first parameter of a remote event is the player. Do this:

game.ReplicatedStorage.talktoevent.OnServerEvent:Connect(function(plr, EA)
	
	print(EA.Value)
end)

Sorry for the late response

Sorry, I already fixed it. I’ll send you the final code if you want to use it! :wink:

I’m alright, but thanks for the offer :smiley: