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

How would you increase a stat when a player touches a brick?

Asked by 5 years ago

I've been trying to change the value of a stat "level" when a brick is touched. I have a LocalScript in the brick with this code:

function OnTouched(Part)
    local level
    print("Touched")
    level.Value = level.Value + 1
end

script.Parent.Touched:Connect(OnTouched)

And this is the code for the leaderboard:

local function  onPlayerJoin(player)
    print("Joined")
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local jumps = Instance.new("IntValue")
    jumps.Name = "Jumps"
    jumps.Value = 0
    jumps.Parent = leaderstats

    local level = Instance.new("IntValue")
    level.Name = "Level"
    level.Value = 0
    level.Parent = leaderstats

    local DebounceTime = .5
    local CanJump = true
    local char = player.Character or player.CharacterAdded:Wait()
    local humanoid = char:WaitForChild("Humanoid")
    humanoid:GetPropertyChangedSignal("Jump"):Connect(function()
        if humanoid.Jump == true and CanJump then
            jumps.Value = jumps.Value + 1
            CanJump = false
            wait(DebounceTime)
            CanJump = true
        end
    end)
end 
game.Players.PlayerAdded:Connect(onPlayerJoin)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remote = Instance.new("RemoteEvent")
Remote.Parent = ReplicatedStorage

Remote.OnServerEvent:Connect(function(player)
  local leaderstats = player:FindFirstChild("leaderstats")
  if leaderstats then
    for _,v in pairs(leaderstats:GetChildren()) do
      v.Value = 0 
    end
  end
end)

Now -- Why doesn't the LocalScript in the brick work at all? I assumed that it was a problem with just level.Value = level.Value + 1. But when I put the print ("Touched") into the script, it never displayed it into the console.

0
LocalScripts don't run in the Workspace; doing a little research would've answered this. https://developer.roblox.com/api-reference/class/LocalScript TheeDeathCaster 2368 — 5y

3 answers

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

The reason this isn't working is because local scripts cannot run in the workspace. They can only run somewhere that is a direct descendant of the player, i.e. StarterPack, StarterGui, or StarterPlayerScripts. The only exception is in the client's character, which is typically achieved by placing the local script in StarterCharacterScripts. To fix this, just put this code in a server script instead of a local script:

function OnTouched(Part)
    local player = game.Players:GetPlayerFromCharacter(Part.Parent)
    if player then
        local leaderstats = player:FindFirstChild("leaderstats")
        if leaderstats then
            leaderstats.Level.Value = leaderstats.Level.Value + 1
        end
    end
end

script.Parent.Touched:Connect(OnTouched)
0
It did change the stat, however I realized I forgot to put in that I only needed it to trigger once. I did try to add it inside of a debounce if/then statement but it made the command not work, so now im stuck again :c Here's a codeshare link to what I came up with as a fix. https://codeshare.io/5Oo9w0 spyderwolf 15 — 5y
0
check the codeshare Gey4Jesus69 2705 — 5y
0
That made the script not work, I added a print bit (Shown in codeshare) that didn't work either. The console gave an error, Part is not a valid member of Workspace though spyderwolf 15 — 5y
0
that was an example.. you need to adapt it to whatever part you're working with. if the script is in the part, change the variable to `local trigger = script.Parent` Gey4Jesus69 2705 — 5y
View all comments (6 more)
0
Oooh, yeah It works now. Btw just to be sure -- script. Disabled lets you rerun the script on a different part later but prevents it running on the same part after it was ran on that part correct? spyderwolf 15 — 5y
0
if you re-enable the script, it will run at that moment. you wont notice anything if its centered around the Touched event like it is, but thats how it works. accept this post if it helped Gey4Jesus69 2705 — 5y
0
Did this answer your question @OP? TheeDeathCaster 2368 — 5y
0
Thanks, and yeah it was answered but I still dont have a button to answer it spyderwolf 15 — 5y
0
^ It's below the answer, "Accept Answer" with a checkmark next to it. For now I'll accept it. TheeDeathCaster 2368 — 5y
0
thanks i am making a sim craftyjr_N8 0 — 3y
Ad
Log in to vote
0
Answered by
Azarth 3141 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

You didn't set "level" to anything and LocalScripts don't run in anything that isn't a descendant of your player. Put it in a regular Script.

local waittime = 3
local debounce = true
-- This debounce variable prevents the script from spamming points

function OnTouched(hit)
    if hit and hit.Parent and debounce then 
        -- Make sure hit is isn't nil and debounce is ready
        debounce = false
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        -- Get the player from whatever bodypart hit the brick.
        if player then 
            -- Make sure a player hit the brick
            local leaderstats = player:FindFirstChild("leaderstats")
            -- Make sure they have the leaderstats Value.
            local level = leaderstats and leaderstats:FindFirstChild("level")
            -- Make sure they have the level Value in leaderstats.
            if level then 
                level.Value = level.Value + 1
                wait(waittime)
            end
        end
        debounce = true
    end
end

script.Parent.Touched:Connect(OnTouched)
0
yours is too complicated craftyjr_N8 0 — 3y
0
Just because it's more complicated doesn't mean it's the wrong answer. Azarth 3141 — 3y
Log in to vote
0
Answered by 3 years ago

I'm not too sure about this, but you didnt set anything for level's variable, meaning that the game doesn't know what level is. Something you CAN do however is change it to:

local level =  leaderstays:FindFirstChild("level")

Also, local scripts only work in startergui, starterplayer, and starterpack.

Answer this question