Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is this touch(part) script not adding XP to the leaderstats?

What I want is when the brick is touched to increase the value XP in my leaderstats. Script in brick:

function onTouch(part) 
    local humanoid = part.Parent:FindFirstChild("Humanoid") 
    if (humanoid ~= nil) then
        game.Players.LocalPlayer.leaderstat.Name ("XP").Status.Value + 5
    end 
end
script.Parent.Touched:connect(onTouch)

2 answers

✓ Accepted answer

That's how you do it

Put a script inside of the part you desire.


game.Players.PlayerAdded:connect(function(player)
local leaderstatVal = player.leaderstats.XP.Value --If this path isn't correct, make the correct path, but it's not "...leaderstat.Name ("XP").Status..." as you used before, it's "...leaderstats["XP"].Status..."
	repeat wait() until player.Character
	script.Parent.Touched:connect(function()
		if player.Character:WaitForChild("Humanoid") then
			leaderstatVal = leaderstatVal + 5
		end 
	end)
end)

Ok it should be working now!

If it works, mark as the solution!!!

Good scripting!

5 comments

0

Are we basically friends now?

0

This does not work, if the part is touched it will add 5 to all the player's leaderstats instead of only the player who touched it. You also used WaitForChild incorrectly and forgot a "then" on an if statement.

0

maybe robloxhighschool xD

0

**I EDITED IT GUYS. SHOULD BE WORKING NOW.

0

Sorry but it actually does not work. Thanks for trying though! :)

There are some syntax errors with your script, and in order to set the leaderboard value to a new one, you must use the equals sign. As well as this, you cannot get the LocalPlayer from a server script; instead you must get it from the character that touched the part.

local statName = "Status" --change to stat name in leaderstats
function onTouch(part)
    local plr = game.Players:GetPlayerFromCharacter(part.Parent)
    if plr then
        local stat = plr.leaderstats[statName]
        stat.Value = stat.Value + 5
    end
end
script.Parent.Touched:Connect(onTouch)

I'm not sure if your stat name is Status or XP, so you can edit the statName variable to set this.

2 comments

0

It still does not work, do you know why?

0

Did you change the statName variable to XP if that is the name of your stat? Otherwise, are there any errors?

This discussion is preserved as it was. New answers, comments, and votes are closed.