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

Assigning Points to All Players with an OnTouched Script?

Asked by 5 years ago
Edited 5 years ago

Hiya! I'm IwuvPikachu and I absolutely suck at scripting. But hey, everyone has to start somewhere... anyways, here's my question:

I'm trying to script a block so that whenever it is touched by another block, it gives a point to everyone on the server.

Here's what my feeble coding ability could come up with:

function onTouched(hit)
    if hit.Name == "Part" then
        local players = game.Players:GetChildren()
        local stats = players:findFirstChild("leaderstats") 
        if stats ~= nil then 
            local Points = stats:findFirstChild("Points") 
            Points.Value  = Points.Value +1
    end
end
script.Parent.Touched:Connect(onTouched)

As you can probably tell, it doesn't work. If you could please point me toward the right method to write this, you would make my day!

I understand that this script will only work if the brick that touches it is explicitly named "Part", and that is okay. If I had to guess, I would say my problem lies in telling the script to add a point to all players on the server? But I'm not sure. Again, thank you in advance for your help!

0
Goodness, that script did not copypaste well. Let me try to fix that for easier reading ^-^" IwuvPikachu 2 — 5y

1 answer

Log in to vote
0
Answered by
Azarth 3141 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

The first is a simple debounce locked to the part in which you're touching. The second is a debounce attached to any player that touches the brick. If you don't want a debounce at all, you can remove it.

local players = game:GetService('Players')
local debounce = true
local debounceTime = 10
-- Every 10 seconds, we're allowed to touch the brick
local points_to_give = 1

local function giveeveryone(points)
    for i,v in pairs(players:GetPlayers()) do 
        local ls = v:findFirstChild("leaderstats")
        if ls then 
            local pointsValue = ls:findFirstChild("Points")
            if pointsValue then 
                pointsValue.Value = pointsValue.Value + points
            end
        end
    end
end

function onTouched(hit)
    local player = players:GetPlayerFromCharacter(hit.Parent)
    -- Gets the player from the part that was touched
    if player then
        if debounce then 
            debounce = false
            giveeveryone(points_to_give)
            wait(debounceTime)
            debounce = true
        end
    end
end

script.Parent.Touched:Connect(onTouched)

local players = game:GetService('Players')
local debounce = {}
-- A debounce is used so we don't spam the points given to the player
-- A table is used so we can use a debounce on the player 
local debounceTime = 10
-- Every 10 seconds, we're allowed to get points
local points_to_give = 1


local function giveeveryone(points)
    for i,v in pairs(players:GetPlayers()) do 
        local ls = v:findFirstChild("leaderstats")
        if ls then 
            local pointsValue = ls:findFirstChild("Points")
            if pointsValue then 
                pointsValue.Value = pointsValue.Value + points
            end
        end
    end
end

function onTouched(hit)
    local player = players:GetPlayerFromCharacter(hit.Parent)
    -- Gets the player from the part that was touched
    if player then
        local lastTouched = debounce[player.Name]
        -- Checks to see if we have touched the brick before
        if lastTouched and tick() - lastTouched > debounceTime or not lastTouched then
            -- Checks when we have touched the brick last 
            -- Checks to see if we touched it more than debounceTime ago
            -- Also runs if we haven't touched the brick before
            giveeveryone(points_to_give)
            debounce[player.Name] = tick()
            -- reset the last time we touched the brick to now
        elseif tick() - lastTouched < debounceTime then
            -- Runs if we tried to touch the brick before 
                -- the alotted debounceTime time
            -- Tells us when we can touch it again
            print( string.format("You need to wait %s seconds before touching the brick again.", math.floor( debounceTime - (tick() - lastTouched) )))
            -- replace %s with the time we have to wait
        end
    end
end

script.Parent.Touched:Connect(onTouched)
0
You are my savior. Thank you so much. IwuvPikachu 2 — 5y
Ad

Answer this question