Storing a NumberValue's Value property.
In the provided code, the value of the NumberValue game.Workspace.ClaimStation.Screen.SurfaceGui.Value is stored in the variable PointsToClaim. However, this does not change the value of the NumberValue itself. The value of PointsToClaim is simply a copy of the value of the NumberValue at the time it is assigned.
If you want to change the value of the NumberValue, you will need to directly modify it using code like game.Workspace.ClaimStation.Screen.SurfaceGui.Value.Value = newValue, or store just the NumberValue object and setting its Value property using NumberValue.Value = newValue.
Protecting functions that utilize debounce.
As for debouncing, it is generally a good practice to wrap the code that does the actual work in a pcall (short for "protected call") when using debouncing, so that if an error occurs, the debounce can be disabled and the function can be called again. This can help to prevent the debounced function from getting stuck in a state where it is constantly on cooldown and unable to execute.
Here is an example of how debouncing could be implemented using a pcall:
local function debouncedFunction()
end
local OnCooldown = false
local CooldownPeriod = 1
local function debounceWrapper(...)
if not OnCooldown then
OnCooldown = true
local success, err = pcall(debouncedFunction, ...)
if not success then
print("Error occurred: " .. tostring(err))
end
wait(CooldownPeriod)
OnCooldown = false
end
end
script.Parent.Touched:Connect(debounceWrapper)
Detecting the player's character.
A more trivial way of detecting a character would be to do this, rather than relying on the parent all the time.
local Players = game:GetService("Players")
script.Parent.Touched:Connect(function(hit)
local character = hit:FindFirstAncestorWhichIsA("Model")
local humanoid = character and character:FindFirstChildWhichIsA("Humanoid")
if character ~= workspace and humanoid then
local player = Players:GetPlayerFromCharacter(character)
if player then
end
end
end)
2 comments
omg you're right, i knew that but i completely forgot when making this script ty for reminding me lol
Glad I could help! Could you please mark this post as an answer so that others who may have the same question can easily find it? I'm always happy to help out when I can. Thanks!