Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Tycoon points claim will not add the value?

Asked by 1 year ago
Edited 1 year ago

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)

1 answer

Log in to vote
1
Answered by 1 year ago
Edited 1 year ago

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()
    -- 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)

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
            -- do stuff here
        end
    end
end)

0
omg you're right, i knew that but i completely forgot when making this script ty for reminding me lol Black_Magic2533 104 — 1y
0
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! pingsockv2 40 — 1y
Ad

Answer this question