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
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)
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
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)