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

Why does the ClickDetector not work?

Hey guys,

So I'm trying to make a part that, when clicked, gives the player a random amount of resources. However, when the part is clicked, nothing happens, but there are no errors. Anyone know whats wrong?

Here's the code: function onclick(player) player.leaderstats.Xp.Value = player.leaderstats.Xp.Value + (math.random(10,50) * (player.leaderstats.Level.Value)) player.leaderstats.Gold.Value = player.leaderstats.Gold.Value + (math.random(250,750)* (player.leaderstats.Level.Value)) player.leaderstats.Shards.Value = player.leaderstats.Shards.Value + math.random(0,1) end script.Parent.ClickDetector.MouseClick:connect(onclick)

1 comment

0

Code Block please. Is FilteringEnabled Enabled?

1 answer

Here's a code block:

function onclick(player) 
    player.leaderstats.Xp.Value = player.leaderstats.Xp.Value + (math.random(10,50) *(player.leaderstats.Level.Value)) 
    player.leaderstats.Gold.Value = player.leaderstats.Gold.Value + (math.random(250,750)* (player.leaderstats.Level.Value)) 
    player.leaderstats.Shards.Value = player.leaderstats.Shards.Value + math.random(0,1) 
end 

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

Here's an improvement:


local function OnClick(Player)
    local Leaderstats = Player:WaitForChild("Leaderstats");
    local Level = Leaderstats:WaitForChild("Level");

	--Xp
    local Xp = Leaderstats:WaitForChild("Xp");
    Xp.Value = Xp.Value + (math.random(10, 50) * Level.Value);
	--Gold
    local Gold = Leaderstats:WaitForChild("Gold");
	Gold.Value = Gold.Value + (math.random(250, 750) * Level.Value);
	--Shards
	local Shards = Leaderstats:WaitForChild("Shards");
	Shards.Value = Shards.Value + math.random(0, 1);
end

script.Parent.ClickDetector.MouseClick:connect(OnClick);

If you're using filtering, then you'll need to register the mouse click event in a local script, then use a RemoteEvent to fire it to a server script, then execute whatever code there.

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