Help making a counter work for a horror game

  1. What do you want to achieve? A horror game where you activate generators. After activating these generators, a GUI will increase a number based on the amount of generators you activated.

  2. What is the issue? I can’t get it to work. No matter what I try. Either it throws an error, does literally nothing, or just bugs my output.

  3. What solutions have you tried so far? _G, that didn’t work because for some reason it doesn’t transfer from server to client. I tried a Module Script, but that just says it can’t require it or doesn’t say anything at all. I’ve also tried various different ways of formatting the script and Module Script.

This is the code in my server script:

local RS = game.ReplicatedStorage
local event = RS.IncreaseGenLocal
local event2 = RS.IncreaseGenServer
require(17422087856)

function OnFired()
	print("client signal recieved, increasing counter and sending signal")
	require(17422087856).counterTableThing.count = require(17422087856).counterTableThing.count + 1
	wait(0.5)
	event2:FireAllClients()
end

event.OnServerEvent:Connect(OnFired)

This is my local script in Starter Player Scripts:

workspace.Generator1:WaitForChild("button")
script.Parent = workspace.Generator1.button.ClickDetector

local cd = script.Parent
local b = script.Parent.Parent
on = false
local RS = game.ReplicatedStorage
local IG = RS.IncreaseGenLocal

local function onClicked(player)
	b.Color = Color3.new(0.145098, 0.972549, 0)
	on = true
	IG:FireServer()
	print("ok, click detected, sending to server")
end

-- Connect the function to the MouseClick event
clickDetector.MouseClick:Connect(onClicked)

This is the script that handles the GUI:

local plr = game.Players.LocalPlayer
local plrgui = plr.PlayerGui
local event = game.ReplicatedStorage.IncreaseGenServer

function GenSwitch()
	wait(0.5)
	print("signal recieved updating gui")
	plrgui.GenCount.TextLabel.Text = "Generators Enabled: "..require(17422087856).counterTableThing.count.."/9"
	local newgui = Instance.new("ScreenGui")
	newgui.Parent = plrgui
	local txtlab = Instance.new("TextLabel")
	txtlab.Parent = newgui
	txtlab.Font = "Creepster"
	txtlab.Text = "Congratulations! A new generator has been enabled!"
	wait(5)
	newgui:Destroy()
end

event.OnClientEvent:Connect(GenSwitch)

And this is the bare bones module script I had to upload just to try and get this thing to work:

local counterTableThing = {}

count = 0

return counterTableThing

Please help me. I’ve been trying for hours.

The documentation for require states:

As noted above, the “object sharing” behavior does not cross the client-server boundary. This means that if a ModuleScript is accessible to both the client and server (such as by being placed in ReplicatedStorage) and require() is called from both a LocalScript as well as a Script, the code in the ModuleScript will be run twice, and the LocalScript will receive a distinct return object from the one received by the Script.

The table you’re accessing to change the counter is not replicated and is instead a local copy for either the server or a specific client. You must use other methods that cross the server-client boundary to send that information to clients. You can pass the count in the RemoteEvent arguments and have clients set their copy of count to the new one provided from the server.

following up from the last bit of their reply, here is how that looks:

the script that updates your gui:

local plr = game.Players.LocalPlayer
local plrgui = plr.PlayerGui
local event = game.ReplicatedStorage.IncreaseGenServer

function GenSwitch(genAmount)
	wait(0.5)
	print("signal recieved updating gui")
	plrgui.GenCount.TextLabel.Text = "Generators Enabled: "..genAmount..."/9" -- removed the require and added in the var that the server passed (seen below)
	local newgui = Instance.new("ScreenGui")
	newgui.Parent = plrgui
	local txtlab = Instance.new("TextLabel")
	txtlab.Parent = newgui
	txtlab.Font = "Creepster"
	txtlab.Text = "Congratulations! A new generator has been enabled!"
	wait(5)
	newgui:Destroy()
end

event.OnClientEvent:Connect(GenSwitch)

the server script:

local RS = game.ReplicatedStorage
local event = RS.IncreaseGenLocal
local event2 = RS.IncreaseGenServer
-- you don't need that module anymore
local genAmount = 0

function OnFired()
	print("client signal recieved, increasing counter and sending signal")
	genAmount += 1 -- is basically the same as (thing = thing + thing)
	wait(0.5)
	event2:FireAllClients(genAmount) -- pass the total amount for clients to display (seen above)
end

event.OnServerEvent:Connect(OnFired)

Thank all of you guys so much! You’ve been a big help.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.