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
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.
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)
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