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

My click detector script works in studio, but not in game, how do I fix this?

Hello, I'm Fako16 and I'm a new scripter that's currently learning. I watched a AlvinBlox video that explained how FE click detectors work and I wanted to make a click detector that would give you resources like wood, stone, etc.. I started making it and it works fine in studio, but I remembered that if it works in studio, that doesn't mean it'll work in game and when I tested it in-game it didn't work. Can someone help me see why it doesn't work?

https://prnt.sc/jo6vc8 PLEASE CHECK THIS IMAGE BEFORE LOOKING AT SCRIPTS!

Script 1 -

game.Players.PlayerAdded:connect(function(player)
	local leaderstats = Instance.new("Folder", player)	
	leaderstats.Name = "leaderstats"	
	
	local wood = Instance.new("IntValue", player)
	wood.Name = "Wood"
	wood.Value = 0
	wood.Parent = leaderstats	
	
	local stone = Instance.new("IntValue", player)
	stone.Name = "Stone"
	stone.Value = 0
	stone.Parent = leaderstats
end)

Script 2 -

game.Workspace.Tree.ClickDetector.MouseClick:connect(function()
	game.Players.LocalPlayer.leaderstats.Wood.Value = game.Players.LocalPlayer.leaderstats.Wood.Value + 1
end)

Script 3 -

local clickdetector = game.Workspace.Tree.ClickDetector
clickdetector.MouseClick:Connect(function()
	game.Workspace.TreeCutting:FireServer()
end)

Any help will be appreciated!

5 comments

0

You don't need FE (Filtered enable) for mouse click as long as the code is in script and not local script

0

I did what you said and it doesn't change the Wood value when i press it and also I need FE to keep my game exploiter-clean

0

I added answer

0

You do need filtering enabled, its used as roblox's anti cheat as well as allowing accounts under 13 to join.

0

Why do you need it for mouse click?

2 answers

So don't add FE for Mouse click as long as everything is done in Script As far as I know mouse click don't need a Remote Event.

You were missing to parent leaderstat.

game.Players.PlayerAdded:connect(function(player)
	 local leaderstats = Instance.new("Folder", player) 
     leaderstats.Name = "leaderstats"   
	 
	 local wood = Instance.new("IntValue", player)
	 wood.Name = "Wood"
	 wood.Value = 0
	 wood.Parent = leaderstats  
	 
	 local stone = Instance.new("IntValue", player)
	 stone.Name = "Stone"
	 stone.Value = 0
	 stone.Parent = leaderstats
	
	 leaderstats.Parent = player --You were missin this
end)

--RiskoZoSlovenska  saw that
so second script should be

Script 2

RiskoZoSlovenska noticed that. So second script should be.

game.Workspace.Tree.ClickDetector.MouseClick:connect(function(player)
	 player.leaderstats.Wood.Value = player.leaderstats.Wood.Value + 1
end)

Well let's look at what happens. Script 1 is run when a player joins, and so there shouldn't be anything wrong with that. Script 3 fires the TreeCutting event to the server. Script 2 does whatever when the ClickDetector is clicked. However, I see several problems here. First of all, script 2 is a Script, and you need a LocalScript to be able to call LocalPlayer. Second, nothing receives the TreeCutting event. Try something like this: Script 1: Script 1 should be a Script placed into Tree

local detec = script.Parent.ClickDetector
detec.MouseClick:Connect(function(playerWhoClicked)
	game.Workspace.TreeCutting:FireClient(playerWhoClicked)
)

This will fire an event to each player who clicked the detector. Then make a script (LocalScript, i believe) inside the player which will receive the event and act upon it. I"m not a professional Lua Scripter, so I probably messed up somewhere, but I think something like this could work. If not, tell me and I'll try to fix it.

2 comments

0

Thank you for your answer! I have fixed the problem by following PlaasBeer's advice although your advice did help me see some errors in my code, thank you very much for this answer! - Fako16

0

No problem! Glad I could help :)

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