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 6 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:

1function OnTouched(Part)
2    local level
3    print("Touched")
4    level.Value = level.Value + 1
5end
6 
7script.Parent.Touched:Connect(OnTouched)

And this is the code for the leaderboard:

01local function  onPlayerJoin(player)
02    print("Joined")
03    local leaderstats = Instance.new("Folder")
04    leaderstats.Name = "leaderstats"
05    leaderstats.Parent = player
06 
07    local jumps = Instance.new("IntValue")
08    jumps.Name = "Jumps"
09    jumps.Value = 0
10    jumps.Parent = leaderstats
11 
12    local level = Instance.new("IntValue")
13    level.Name = "Level"
14    level.Value = 0
15    level.Parent = leaderstats
View all 43 lines...

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 — 6y

3 answers

Log in to vote
1
Answered by 6 years ago
Edited 6 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:

01function OnTouched(Part)
02    local player = game.Players:GetPlayerFromCharacter(Part.Parent)
03    if player then
04        local leaderstats = player:FindFirstChild("leaderstats")
05        if leaderstats then
06            leaderstats.Level.Value = leaderstats.Level.Value + 1
07        end
08    end
09end
10 
11script.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 — 6y
0
check the codeshare Gey4Jesus69 2705 — 6y
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 — 6y
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 — 6y
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 — 6y
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 — 6y
0
Did this answer your question @OP? TheeDeathCaster 2368 — 6y
0
Thanks, and yeah it was answered but I still dont have a button to answer it spyderwolf 15 — 6y
0
^ It's below the answer, "Accept Answer" with a checkmark next to it. For now I'll accept it. TheeDeathCaster 2368 — 6y
0
thanks i am making a sim craftyjr_N8 0 — 4y
Ad
Log in to vote
0
Answered by
Azarth 3141 Moderation Voter Community Moderator
6 years ago
Edited 6 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.

01local waittime = 3
02local debounce = true
03-- This debounce variable prevents the script from spamming points
04 
05function OnTouched(hit)
06    if hit and hit.Parent and debounce then
07        -- Make sure hit is isn't nil and debounce is ready
08        debounce = false
09        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
10        -- Get the player from whatever bodypart hit the brick.
11        if player then
12            -- Make sure a player hit the brick
13            local leaderstats = player:FindFirstChild("leaderstats")
14            -- Make sure they have the leaderstats Value.
15            local level = leaderstats and leaderstats:FindFirstChild("level")
View all 26 lines...
0
yours is too complicated craftyjr_N8 0 — 4y
0
Just because it's more complicated doesn't mean it's the wrong answer. Azarth 3141 — 4y
Log in to vote
0
Answered by 4 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:

1local level =  leaderstays:FindFirstChild("level")

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

Answer this question