How to make a game wide variable

place it in a Script. place Script in ServerScriptService

image
lol

Ok did that, but how will I display this on a surface Gui and setup a trigger for it?

local DS = game:GetService("DataStoreService"):GetGlobalDataStore()
local VarName = "Players Fell"
local CurrentVal = 0
local AddVal = 0
local Label = --put your TextLabel object here

local function Add()
  local success, msg = pcall(function()
    CurrentVal = DS:IncrementAsync(VarName, AddVal)
  end)
  if not success then print("----", msg) end
  if success then AddVal = 0 Label.Text = tostring(CurrentVal) end
end
while wait(10) do
  Add()
end

Try this.

local DS = game:GetService("DataStoreService"):GetGlobalDataStore()
local VarName = "Players Fell"
local CurrentVal = 0
local AddVal = 0
local Label = --put your TextLabel object here

local function Update()
  local success, msg = pcall(function()
    CurrentVal = DS:IncrementAsync(VarName, AddVal)
  end)
  if not success then print("----", msg) end
  if success then AddVal = 0 Label.Text = tostring(CurrentVal) end
end
local function Add1()
  AddVal = AddVal + 1
  Label.Text = tostring(CurrentVal + AddVal)
end

-- trigger Add1() somehow here

while wait(10) do
  Update()
end

Ok, so how would I add the trigger part

One way would be just everytime a player dies or resets.

game.Players.PlayerAdded:Connect(function(player)
  player.CharacterAdded:Connect(function(char)
    local hum = char:WaitForChild("Humanoid")
    hum.Died:Connect(function()
      Add1()
    end)
  end)
end)

I’m looking for when a player passes through a certain object in workspace

Use the following as a trigger.

local deb = {}
workspace.Object.Touched:Connect(function(obj)
  local h = obj.Parent.Humanoid
  if deb[h] then return else deb[h] = true end
  h:BreakJoints()
  Add1()
  wait(10)
  deb[h] = nil
end)

Does this apply in studio, or only Roblox itself?

you need to Enable studio access to the DataStore for it to work in studio

I did that but it’s not incrementing.

did you create a surfaceGui and TextLabel and then do something similar to

local Label = workspace.Display.SurfaceGui.TextLabel

?

like tell me errors and stuff otherwise it’s harder to give good feedback.

Yes I did Idk
image

local DS = game:GetService("DataStoreService"):GetGlobalDataStore()
local VarName = "Players Fell"
if game:GetService("RunService"):IsStudio() then
	VarName = "Players Fell Studio"
end
local CurrentVal = 0
local AddVal = 0
local Label = workspace.Display.SurfaceGui.TextLabel
local function Update()
	local success, msg = pcall(function()
		CurrentVal = DS:IncrementAsync(VarName, AddVal)
	end)
	if not success then print("----", msg) end
	if success then AddVal = 0 Label.Text = tostring(CurrentVal) end
end
local function Add1()
	AddVal = AddVal + 1
	Label.Text = tostring(CurrentVal + AddVal)
end

local deb = {}
workspace.Object.Touched:Connect(function(obj)
	local h = obj.Parent.Humanoid
	if deb[h] then return else deb[h] = true end
	h:TakeDamage(h.MaxHealth)
	Add1()
	wait(10)
	deb[h] = nil
end)

while wait(10) do
	Update()
end

This code with a part named Object in Workspace,
a part named Display in Workspace,
SurfaceGui named SurfaceGui in Display,
TextLabel named TextLabel in SurfaceGui.

The code being in a Script in ServerScriptService.

1 Like