How to make a script not global?

Hi,

How do I make this not global and be for every player individually?

local Time = 300 

local db = 0
local timedisplay = script.Parent.Timer.TextLabel

script.Parent.Touched:Connect(function(hit) 
	if db == 0 then

		if hit.Parent:FindFirstChild("Humanoid") then

			task.spawn(function()
				db = Time
				for i = 1, Time do 
					task.wait(1)
					db -= 1 
					timedisplay.Text = tostring(db)
				end 
			end)

			local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
			plr.leaderstats.Gold.Value = plr.leaderstats.Gold.Value + 50	
		end
	end
end)

Scripts are global. LocalScripts are local.

2 Likes

Use localscripts. This video explains it well.

1 Like

I changed it to a local script, but how do I add a debounce for each player?

the debounce will be for every player (locally of course)

2 Likes

Yes, but it doesn’t add anything to leaderstats because it’s not on the server.

It should work, adding values in leaderstats also work in servers

If you’d like, use remote events.

1 Like

Should work.

local Time = 300 
local Activated = false
local db = 0
local timedisplay = script.Parent.Timer.TextLabel

script.Parent.Touched:Connect(function(hit)
if not Activated then
Activated = true
	if db == 0 then

		if hit.Parent:FindFirstChild("Humanoid") then

			task.spawn(function()
				db = Time
				for i = 1, Time do 
					task.wait(1)
					db -= 1 
					timedisplay.Text = tostring(db)
				end 
			end)

			local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
			plr.leaderstats.Gold.Value = plr.leaderstats.Gold.Value + 50	
		end
	end
wait(2) -- Delay
Activated = false
end
end)

Please do not listen to other replies in this section. They are either recommending you to use localscripts for leaderstats (garbage) or using “global” debounces.

You should use these debounces:

local debounces = {}

function Do(userId)
     if debounces[userId] == true then return end
     debounces[userId] = true
     --Code here
     debounces[userId] = false
end

This is a basic demonstration, if you want to use time your debounce code should vary.

I can also see that you are trying to update textlabels, use remoteevents for this

2 Likes

Im not sure if youre reading the topic correctl
Locally is the Client, aka LocalScript

Did you even read his script before replying?
He’s trying to change leaderstats values to be saved, that won’t work on localscripts

Yes, i have, and they are asking how do you do it locally, plus, a debounce

Not to forget that your script doesn’t change anything except adding a boolean debounce and a wait that should be task.wait instead

they’ve never said anything “locally” AFAIK, the topic creator didn’t use the correct words for the topic but by reading the script you can clearly see he’s not attempting to make a leaderstat script “local”

I am Pretty sure they did say that
“Not Global”

Use a RemoteEvent to update leaderstats.

AlvinBlox has a great video on RemoteEvents

In this topic, he’s referring to global as “a player debounce and not a script debounce”

In this case the remoteevent should be used to update the textlabel,not the leaderstats. Very exploitable if using what you recommended

Doesnt really appear to be the case, but i see your point