Im trying to make a tycoon game just to learn ig without using free models but i came across something weird when making the script for the points claim system, for some reason i get no errors, the color of the platform changes and everything but the points dont go to the player.
ServerScript inside of the sell platform
local PointsToClaim = game.Workspace.ClaimStation.Screen.SurfaceGui.Value.Value local Cooldown = 1 local OnCooldown = false script.Parent.Touched:Connect(function(part) if OnCooldown == false then if part.Parent:FindFirstChild("Humanoid") then local player = game.Players:GetPlayerFromCharacter(part.Parent) player.leaderstats.Points.Value += PointsToClaim PointsToClaim = 0 OnCooldown = true script.Parent.Color = Color3.new(1, 0, 0) wait(Cooldown) OnCooldown = false script.Parent.Color = Color3.new(0.341176, 1, 0.160784) end end end)
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
.
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() -- Do some work here 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)
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 -- do stuff here end end end)