UserInputService Tool Problem

I was trying to make an input to select random NPCs for a tool and for some reason it did not work and i have no clue how to fix it.

Here’s my code:

local KeyBinding = game:GetService("UserInputService")

KeyBinding.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		script.Parent.ChangeNPC:Fire()
	end
end)

local SelectedNPC = script.Parent.Selected

script.Parent.ChangeNPC.Event:Connect(function()
	game.Players:GetPlayerFromCharacter().Character:FindFirstChildOfClass("Humanoid"):LoadAnimation(script.Parent.Animations.Change):Play()
end)

script.Parent.Activated:Connect(function()
	game.Players:GetPlayerFromCharacter().Character:FindFirstChildOfClass("Humanoid"):LoadAnimation(script.Parent.Animations.Use):Play()
end)



game.Players:GetPlayerFromCharacter().Character:FindFirstChildOfClass("Humanoid"):LoadAnimation(script.Parent.Animations.Change).GetMarkerReachedSignal(game.Players:GetPlayerFromCharacter().Character:FindFirstChildOfClass("Humanoid"):LoadAnimation(script.Parent.Animations.Change), "ChangeEvent"):Connect(function()
	local NPCStorage = game.ServerStorage.NpcSpellStorage:GetChildren()
	local ChosenNPC = NPCStorage[math.random(1, #NPCStorage)]
	script.Parent.Part.Change:Play()

	SelectedNPC.Value = ChosenNPC

	script.Parent.Part.Color = SelectedNPC.Torso.Color
	script.Parent.Part.Face.Texture = SelectedNPC.Head:FindFirstChildOfClass("Decal")
end)

game.Players:GetPlayerFromCharacter().Character:FindFirstChildOfClass("Humanoid"):LoadAnimation(script.Parent.Animations.Use).GetMarkerReachedSignal(game.Players:GetPlayerFromCharacter().Character:FindFirstChildOfClass("Humanoid"):LoadAnimation(script.Parent.Animations.Use), "CloneEvent"):Connect(function()
	local Part = script.Parent.Part
	if math.random(1, 500) == 1 then
		script.Parent.Part["Clone (Rare)"]:Play()
	else
		script.Parent.Part.Clone:Play()
	end
	local Explode = Instance.new("Explosion")
	Explode.Parent = workspace
	Explode.BlastPressure = 0
	Explode.BlastRadius = 0

	local Clone = SelectedNPC:Clone()
	Clone.Parent = workspace
	Clone:PivotTo(script.Parent.Part)
end)

Any Solutions?

1 Like

in what way is it not working? are there any errors in the console? if its simply not doing anything then you need to put print statements before/after lines of code to see if they are running

1 Like

No Errors at all Actually, and give me a solution please

this is a localscript or not??

i’m using Regular script actually and i realized, local script is required for a User input survive. i think i figure it out.

To access to UserInputService you must be in a local script.

i know and i figured out how to fix it

Can you show the server script?

it did not work or server script, it only work for local script actually. and i encountered an error

What is the error and can you please send the server script?

i actually encountered an error " Argument 1 missing or nil" and i was actually using local script

what line? in the sended error

Your code is a bit messy, but I think I understand what you’re trying to do. Ensure that this is a LocalScript, so that we can access Players.LocalPlayer and use UserInputService. Good luck!

local KeyBinding = game:GetService("UserInputService")
local playerCharacter = game.Players.LocalPlayer.Character
local humanoid = playerCharacter:FindFirstChildOfClass("Humanoid")

KeyBinding.InputBegan:Connect(function(input, processed)
    if processed then return end
	if input.KeyCode == Enum.KeyCode.E then
		script.Parent.ChangeNPC:Fire()
	end
end)

local SelectedNPC = script.Parent.Selected

script.Parent.ChangeNPC.Event:Connect(function()
	humanoid:LoadAnimation(script.Parent.Animations.Change):Play()
end)

script.Parent.Activated:Connect(function()
	humanoid:LoadAnimation(script.Parent.Animations.Use):Play()
end)

humanoid:LoadAnimation(script.Parent.Animations.Change).GetMarkerReachedSignal("ChangeEvent"):Connect(function()
	local NPCStorage = game.ServerStorage.NpcSpellStorage:GetChildren()
	local ChosenNPC = NPCStorage[math.random(1, #NPCStorage)]
	script.Parent.Part.Change:Play()

	SelectedNPC.Value = ChosenNPC

	script.Parent.Part.Color = SelectedNPC.Value.Torso.Color
	script.Parent.Part.Face.Texture = SelectedNPC.Value.Head:FindFirstChildOfClass("Decal").Texture
end)

humanoid:LoadAnimation(script.Parent.Animations.Use).GetMarkerReachedSignal("CloneEvent"):Connect(function()
	local Part = script.Parent.Part
	if math.random(1, 500) == 1 then
		script.Parent.Part["Clone (Rare)"]:Play()
	else
		script.Parent.Part.Clone:Play()
	end
	local Explode = Instance.new("Explosion")
	Explode.BlastPressure = 0
	Explode.BlastRadius = 0
    Explode.Position = Vector3.zero() -- Please set the explosion's position
	Explode.Parent = workspace

	local Clone = SelectedNPC.Value:Clone()
	Clone.Parent = workspace
	Clone:SetPrimaryPartCFrame(Part.CFrame)  -- Set the position of the cloned NPC
end)

But unfortunately i got another error it’s called “attempt to index nil with ‘FindFirstChildOfClass’” can you fix that up?

and also i will use an WaitForChild method and i got Exactly The Same Error. and i have no clue how to fix that.