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

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

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)

1 comment

0

Humanoid for the click would be, click.Character.Humanoid

2 answers

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.

1 comment

0

automatically gives you the player.* Sorry.

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)

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