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