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

Why does Touched work to get points but Click does not work to get points?

Asked by 4 years ago

I have typed a code that gives you points when you touch it with help from peasfactory

enabled = false

script.Parent.Touched:Connect(function(hit)
    local humanoid = hit.Parent:FindFirstChild("Humanoid")
    if humanoid ~= nil and enabled == false then
        enabled = true
        script.Parent.BrickColor = BrickColor.new("Really red")
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        local points = player.leaderstats.Points
        points.Value = points.Value + 5
        wait(3)
        enabled = false
        script.Parent.BrickColor = BrickColor.new("Sea green")
    end
end)

But When I try to change it to a click detector it doesn't work and tells me the player is nil

enabled = false

script.Parent.ClickDetector.MouseClick:Connect(function(click)
    local humanoid = click:FindFirstChild("Humanoid")
    if humanoid ~= nil and enabled == false then
        script.Parent.BrickColor = BrickColor.new("Really red")
        enabled = true
        local player = game.Players:GetPlayerFromCharacter(click)
        local points = player.leaderstats.Points
        points.Value = points.Value + 5
        wait(3)
        enabled = false
        script.Parent.BrickColor = BrickColor.new("Sea green")
    end
end)
0
Humanoid for the click would be, click.Character.Humanoid PastDays 108 — 4y

2 answers

Log in to vote
0
Answered by
pwx 1581 Moderation Voter
4 years ago

Okay, I see what you've tried to done here and you almost did it.

You see, ClickDetector always gives the player variable.

Example:

script.Parent.ClickDetector.MouseClick:Connect(function(player)

There is no need to use GetPlayerFromCharacter since it automatically gives you the character.

0
automatically gives you the player.* Sorry. pwx 1581 — 4y
Ad
Log in to vote
0
Answered by
U_srname 152
4 years ago

Your script should look like this:

local enabled = false

script.Parent.ClickDetector.MouseClick:Connect(function(player)
    if enabled == false then
        script.Parent.BrickColor = BrickColor.new('Really red')
        enabled = true
        local points = player.leaderstats.Points
        points.Value = points.Value + 5
        wait(3)
        enabled = false
        script.Parent.BrickColor = BrickColor.new('Sea green')
    end
end)

Answer this question