Help please with picked.Name

i want the npc to say “Kill butter5432m for 15 coins” using my name as an example
script:

local players = game:GetService("Players"):GetPlayers()
local picked = players[math.random(1, #players)]
local plr = game.Players.PlayerAdded:Connect(function(plr)
	local char = plr.Character or plr.CharacterAdded:Wait(1)
	local hum = char:WaitForChild("Humanoid")
	local rep = game:GetService("ReplicatedStorage")
	local rem = rep:WaitForChild('RemoteEvent')
	local chat = game:GetService("Chat")
	
	local mouse = script.Parent
	local debounce = false
	mouse.MouseClick:Connect(function()
		if debounce == false then
		debounce = true
			wait(0.1)

			chat:Chat(script.Parent.Parent.Head,"Kill"picked.Name,"White")
			rem:FireClient(plr)
			
			print("working")
			wait(30)
			debounce = false
		end
	end)
end)

the script is inside a clickdetector inside an npc

In order to combine strings you must use double dot:
chat:Chat(script.Parent.Parent.Head, "Kill "..picked.Name, "White")

1 Like

script:

local players = game:GetService("Players"):GetPlayers()
local picked = players[math.random(1, #players)]
local plr = game.Players.PlayerAdded:Connect(function(plr)
	local char = plr.Character or plr.CharacterAdded:Wait(1)
	local hum = char:WaitForChild("Humanoid")
	local rep = game:GetService("ReplicatedStorage")
	local rem = rep:WaitForChild('RemoteEvent')
	local chat = game:GetService("Chat")
	
	local mouse = script.Parent
	local debounce = false
	mouse.MouseClick:Connect(function()
		if debounce == false then
		debounce = true
			wait(0.1)
			chat:Chat(script.Parent.Parent.Head, "Kill "..picked.Name, "White")
			rem:FireClient(plr)
			
			print("working")
			wait(30)
			debounce = false
		end
	end)
end)
	

output message:

Workspace.Dummy.ClickDetector.Script:2: invalid argument #2 to 'random' (interval is empty) 

the script is located in a clickdetector in the dummy
normal script*

can you explain yourself better

you can use chat:Chat(script.Parent.Parent.Head, "Kill "…players[picked].Name, “White”)

to print random player name

1 Like

Refresh player list every time you use clickdetector since line 2 code executes before player instance was added

1 Like

oh sorry i didin’t see you took it alraedy

1 Like

what?

still no work

script:


local plr = game.Players.PlayerAdded:Connect(function(plr)

	local char = plr.Character or plr.CharacterAdded:Wait(1)
	local hum = char:WaitForChild("Humanoid")
	local rep = game:GetService("ReplicatedStorage")
	local rem = rep:WaitForChild('RemoteEvent')
	local chat = game:GetService("Chat")
	
	local mouse = script.Parent
	local debounce = false
	mouse.MouseClick:Connect(function()
		local players = game:GetService("Players"):GetPlayers()

		local picked = players[math.random(1, #players)]
		if debounce == false then
		debounce = true
			wait(0.1)
			chat:Chat(script.Parent.Parent.Head, "Kill "..players[picked].Name, "White")
			rem:FireClient(plr)
			
			print("working")
			wait(30)
			debounce = false
		end
	end)
end)

output error:

Workspace.Dummy.ClickDetector.Script:19: attempt to index nil with 'Name'

picture the npc shown below says
uiiuupwv5mi71

kill (the random player chosen ) for 15 coins?
and then after it say that a gui pop up and when u press ok the npc will say ok also in chat form
i already have the gui ready
Screenshot (131)

Picked is already player instance, replace players[picked].Name with picked.Name

1 Like