Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does this not work? -simple

Its supposed to say PLAYER died when you press it. It works fine when its just "Died" Is there something wrong.

I have already tried p = hit.Characterand label.Text = p.. "Died!"

local label = script.Parent.SurfaceGui.TextLabel

function Onclick(hit)
		

	label.Text = hit.Character.. "Died!"
	wait(.1)
	hit.Character.Head:remove()
end

script.Parent.ClickDetector.MouseClick:connect(Onclick)

2 answers

✓ Accepted answer

Your Problem

The MouseClick event returns the player that clicked. So when you index Character and try to concatenate it with a string it's going to throw an error like;

Attempt to concatenate a userdata

This happens because the Character itself is a userdata. You need to index the Name property - a string(:


Code

local label = script.Parent.SurfaceGui.TextLabel

function Onclick(hit)
	local char = hit.Character --Define the character here
	label.Text = char.Name.. "Died!" --Index 'Name'
	wait(.1)
	char.Head:Destroy() --'remove' is deprecated, use Destroy!
end

script.Parent.ClickDetector.MouseClick:connect(Onclick)

2 comments

0

You say Remove is depreciated. Remove still works. For those like myself, I prefer remove over destroy. :P

0

The remove function just sets the object's parent to nil. It doesn't actually garbage collect it. The data is still there meaning any script(s) that were running in it will still run, as well as increased lag for the server if it's used excessively.

Problem: You never asked for the players name, just their physical character. Solution:

i = Instance.new("ClickDetector")
i.Parent = script.Parent
local label = script.Parent.SurfaceGui.TextLabel
function Onclick(hit)

label.Text = hit.Character.Name.. "Died!"
wait(.1)
hit.Character.Head:Destroy() -- remove is deprecated
end
script.Parent.ClickDetector.MouseClick:connect(Onclick)

2 comments

0

Yeah, i forgot .Name ;)

0

XD REMEMBER TEH NAME!

This discussion is preserved as it was. New answers, comments, and votes are closed.