function addxp(hit) local dude = hit.Parent:WaitForChild('Character') dude.xp.Value = 1 script.Parent:Destroy() end script.Parent.Touched:connect(addxp) -- the script's parent is a brick
Im 90% sure this should work, but when i touch the brick absolutely nothing happens. I checked the values after too, it remains at 0. Anything I did wrong?
If you are trying to access the player's Character
, check if it has a Humanoid
.
function addxp(hit) local dude = hit.Parent:WaitForChild("Humanoid").Parent dude.xp.Value = 1 script.Parent:Destroy() end script.Parent.Touched:connect(addxp)
or you can use the GetPlayerFromCharacter
event.
function addxp(hit) local dude = game.Players:GetPlayerFromCharacter(hit.Parent).Character -- accessing the player through the character dude.xp.Value = 1 script.Parent:Destroy() end script.Parent.Touched:connect(addxp)
You're leaving out some needed information. Is "xp" something in the character or in the leaderboards?
Anyway, I'll do both.
In character:
script.Parent.Touched:connect(function(hit) local dude = hit.Parent:WaitForChild("Humanoid").Parent --Wait for the humanoid, checking if "hit" is a character local xp = dude:WaitForChild("xp") xp.Value = 1 end)
In player:
script.Parent.Touched:connect(function(hit) local dude = hit.Parent:WaitForChild("Humanoid").Parent local player = game.Players:GetPlayerFromCharacter(dude) local leaderstats = player:WaitForChild("leaderstats") local xp = leaderstats:WaitForChild("xp") xp.Value = 1 end)
Hope this helps! :)
There cannot be found thing such as Character. Character can be found from game.Players.Player.Character. You should also add the xp To Leaderboards, check out how to in wiki if you don't now: http://wiki.roblox.com/index.php?title=Leaderboards
function getxp(hit) for i,Player in ipairs(game.Players:GetPlayers()) do if hit.Parent = Player.Character then local Character = hit.Parent local Player = game.Players:GetPlayerFromCharacter(Character) Character.xp.Value = Character.xp.Value +1 --I made this like this, you can do like you want script.Parent:Destroy() end end script.Parent.Touched:connect(getxp)