Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-1

Why does this not work? -simple

Asked by
iNicklas 215 Moderation Voter
9 years ago

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

Log in to vote
2
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

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)
0
You say Remove is depreciated. Remove still works. For those like myself, I prefer remove over destroy. :P Tkdriverx 514 — 9y
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. Goulstem 8144 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

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)
0
Yeah, i forgot .Name ;) iNicklas 215 — 9y
0
XD REMEMBER TEH NAME! viralmoose 65 — 9y

Answer this question