Click script is not working, why?

I am making a script that when you click the block it makes a gui show up. Why is it not working?
Type: Script (not local)
location: game>workspace>Folder>Stone>Main>ClickDetector
the script:

local GotStone = false
variables = require(game.ServerScriptService.Variables)
script.Parent.MouseClick:Connect(function()
	variables.CurrentStone = variables.CurrentStone +1
	
end)
if variables.CurrentStone >= 1 then
	GotStone = true
end
if GotStone == true then
game.StarterGui.CollectionBook.MainFrame.Stone.Tick.Visible = true
end

You should probably fire an event to the client instead, and have a LocalScript inside the GUI receive the event.

1 Like

But why isnt it working, whats the error?

There isn’t an error within the script, it’s a logical error. Add a RemoteEvent to ReplicatedStorage, then fire the event to the client. Then in the LocalScript, you would have something like:

local GUI = script.Parent

game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function()
     GUI.MainFrame.Stone.Tick.Visible = true
end)

I may be wrong.

1 Like

StarterGui does not contain the player’s GUI. It’s a service, and when a player joins, everything in it is cloned to the player’s GUI. Now how do we access the player’s GUI? Well, by doing player.PlayerGui instead of game.StarterGui. Example:

local GotStone = false
variables = require(game.ServerScriptService.Variables)
script.Parent.MouseClick:Connect(function(player)
	variables.CurrentStone = variables.CurrentStone +1
	
end)
if variables.CurrentStone >= 1 then
	GotStone = true
end
if GotStone == true then
player.PlayerGui.CollectionBook.MainFrame.Stone.Tick.Visible = true
end
2 Likes

Would player be like:

local player = game.Players.LocalPlayer

Adding onto what @ND_B7 said, if you were to fire an event, you would do
script.Parent.MouseClick:Connect(function(player)

then you would fire the event like this:

if GotStone == true then
     game.ReplicatedStorage.RemoteEvent:FireClient(player)
end
1 Like

Is this a correct script?

local player = game.Players.LocalPlayer
local GotStone = false
variables = require(game.ServerScriptService.Variables)
script.Parent.MouseClick:Connect(function(player)
	variables.CurrentStone = variables.CurrentStone +1
	
end)
if variables.CurrentStone >= 1 then
	GotStone = true
end
if GotStone == true then
player.PlayerGui.CollectionBook.MainFrame.Stone.Tick.Visible = true
end

No, because LocalPlayer doesn’t exist on regular Scripts which run on the server, not each client. If you wanted to use a LocalScript, that wouldn’t work either because ServerScriptService doesn’t exist on the client. You would use the player parameter that you get from the event:

local GotStone = false
variables = require(game.ServerScriptService.Variables)
script.Parent.MouseClick:Connect(function(player)
	variables.CurrentStone = variables.CurrentStone +1
if variables.CurrentStone >= 1 then
	GotStone = true
end
if GotStone == true then
player.PlayerGui.CollectionBook.MainFrame.Stone.Tick.Visible = true
end

end)
1 Like

Great thanks to all. It works now, happy scripting!

2 Likes