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

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

Asked by
Fako16 13
6 years ago
Edited 6 years ago

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!

0
You don't need FE (Filtered enable) for mouse click as long as the code is in script and not local script PlaasBoer 275 — 6y
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 Fako16 13 — 6y
0
I added answer PlaasBoer 275 — 6y
0
You do need filtering enabled, its used as roblox's anti cheat as well as allowing accounts under 13 to join. xXiNotoriousXx 31 — 6y
0
Why do you need it for mouse click? PlaasBoer 275 — 6y

2 answers

Log in to vote
0
Answered by
PlaasBoer 275 Moderation Voter
6 years ago
Edited 6 years ago

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)
Ad
Log in to vote
0
Answered by 6 years ago

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.

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 Fako16 13 — 6y
0
No problem! Glad I could help :) RiskoZoSlovenska 378 — 6y

Answer this question