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

How To Make Click Detector Add Coins?

How To Make Click Detector Add Coins? I'll put both scripts here to help me where I'm wrong.

Leaderstats: (ServerScriptService)

game.Players.PlayerAdded:Connect(function(plr)
local lead = Instance.new("Model")
lead.Name = "leaderstats"
lead.Parent = plr
--
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Value = 0
Coins.Parent = lead
end)

ClickForCoins: (Workspace)

function onclick(hit)
local plr = hit.Name
local get = game.Players:FindFirstChild(plr)
if get ~= nil and script.Parent.CanClick.Value == 1 then
script.Parent.CanClick.Value = 0
get.leaderstats.Coins.Value = get.leaderstats.Coins.Value +10 -- Change 10 To How Much Per Click
wait(1)
script.Parent.CanClick.Value = 1
end
end

2 comments

1

Did you ever connect this function? I don't see it being called anywhere

0

Please use a Folder Object, not a Model.

1 answer

✓ Accepted answer

You need to actually bind your add function to a .MouseClick listener to run it when the Player activates the ClickDetector Object. You're also thinking of the wrong RBXScriptSignal, ClickDetectors do not act the same as a .Touched event—no Hit parameter is returned, nor should it be filtered for... the .MouseClick signal returns the Player Object that initiated the event, so you already have immediate access.


Note:

Using a numerical ValueObject is not recommended when debouncing. It is suggested debounces should be operated via Boolean logic, so please replace your ValueObject with a BoolValue Instance


Ensure the script resides within the ClickDetector Object, and is a SeverScript Instance.

local CanClick = script.Parent:WaitForChild("CanClick")

local ClickDetector = script.Parent

local function onClick(playerClicked)
	if (playerClicked and CanClick.Value ~= false) then
		CanClick.Value = false
		local Leaderstats = playerClicked:FindFirstChild("leaderstats")
		if (Leaderstats and Leaderstats.Coins) then
			Leaderstats.Coins.Value = (Leaderstats.Coins.Value + 10)
		end
		wait(1)
		CanClick.Value = true
	end
end

ClickDetector.MouseClick:Connect(onClick)

EDIT:

This is a proper leaderstat constructor Script, please apply this:

local players = game.Players

players.PlayerAdded:Connect(function(player)
	local leaderstatFolder = Instance.new("Folder")
	leaderstatFolder.Name = "leaderstats"
	leaderstatFolder.Parent = player

	local coinsValue = Instance.new("IntValue")
	coinsValue.Name = "Coins"
	coinsValue.Value = 0
	coinsValue.Parent = leaderstatFolder
end)

If this helps, don't forget to accept this answer!

10 comments

0

W001: Unknown global ('player')

0

I resolved my issue above, however you should've caught this: I use the wrong variable name. I expect you're a beginner, yet I suggest that you try to learn more about Lua so you don't run into these issues and require help.

0

I will accept the answer when I manage to make it work, hopefully soon, I will be back soon. it should probably be changed locally by CanClick, local CanClick = workspace.Parent: WaitForChild ("Humanoid") however, even if I fail to do so, I will complete the answer for the help given. probably a screenshot helps, because yes, I think I'm pretty beginner and I'm just trying to catch myself.

0

CanClick is supposed to allocate the BoolValue within the ClickDetector, not a Humanoid, simply revert it to the code above, and it should work properly. You'll also never find a Humanoid as a Child of workspace, they're commonly Descendants when found in Characters. There is also no property named Value within Humanoid Objects so the Script will error.

0

I just tested my Script, it works just fine

0

I've supplied a proper Leaderstat Program, use that to solve your issue.

0

even now I didn't manage to make work, I gave copy-paste to everything :( I think the problem is for me no such collection script works when I click.

1

now it works, you said something about Using a numerical ValueObject is not recommended when debouncing. so please replace your ValueObject with a BoolValue Instance, so I changed IntValue to BoolValue, so that doesn't work. now I changed CanClick to IntValue from BoolValue, and it works!

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