Player name script not work

I cant get any error.

local player = game.Players.LocalPlayer
local playerName = player.Name
local head = workspace.Dummy.Head

part.TouchEnded:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid then
    ChatService:Chat(head, "Hi devforum you like Bloxy Cola " .. playerName .. "?", "Red")
	end
end)

Guessing by the script, are you trying to make the part say something when you stop touching it? Because I’m kinda confused on what you’re trying to say.

1 Like

What is wrong with the code? And what are you trying to achieve. Please state your problem more clearly so we can point you in the right way.

1 Like

@Jacky2804 @Haf_p Sorry this video tells it better: 2023 03 07 15 36 25 - YouTube

Thank you for the video. Also one more thing. Is the script a local script or a server sided script?

1 Like

This server sided experience script.

You can’t use game.Players.LocalPlayer on the server. Also, next time it would help if you explained what you’re problem is, and what you’re trying to do, so people can help you.

local head = workspace.Dummy.Head

part.TouchEnded:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player then
        ChatService:Chat(head, `{player.Name}, you like Bloxy Cola?`, "Red")
	end
end)
1 Like

Yes, I will pay attention to this in my next help topics.

Thanks, it worked, if I used localscript, would that code work?

Yes, but the local player will also detect touches from other players. You’d have to only check for your own in the script.

This game is a game for 1 person, I think I needed to use localscript.

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