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

Award Leaderstats not working? Please help.. [Answered]

I have 2 scripts inside 1 part... the first script insert a Int. Value and that script works fine... I want this to be a script that when you touch it it changes the leaderstats Value Please help

function onTouched(hit)
	local player = hit.Parent
	Logs = player.leaderstats.Logs
	Logs.Value = Logs.Value + 5
end
script.Parent.Touched:connect(onTouched)

2 comments

0

Check mine. It should work.

0

How is this -1 point?

3 answers

✓ Accepted answer

Add this to workspace as the leaderboard.

game.Players.PlayerAdded:connect(function(player)
    local stats = Instance.new("IntValue", player)
    stats.Name = "leaderstats"

    local logs = Instance.new("IntValue", stats)
    logs.Name = "Logs"
    logs.Value = 0
end)

Put inside part

game.Players.PlayerAdded:connect(function(player)
	script.Parent.Touched:connect(function(hit)
	    local logs = player.leaderstats.Logs
	    logs.Value = logs.Value + 5
	    wait(2) -- To stop constant spamming
	end)
end)

This should work as it has worked for me. Hope I helped and please leave a rate up and accept answer for everyone else.

- NinjoOnline

The problem with your script is that A) You're assuming that ANYTHING that touches that part, is part of a Player. It could just be a random part that falls on it. :P and B) The line local player = hit.Parent

What's wrong with that is that, say I go and touch the part, Okay? It'll just say that my Leg/Arm's Parent is a Player, when it's ACTUALLY my character. (Unless you keep your leaderstats in people's character, then all of this is wrong. :c )

So, first you need to make sure it's a Player, and then get the Player. You can do both of these, by just using the :GetPlayerFromCharacter() method, in Players, like so.

Players = game:GetService("Players") --// To get the Players. Just 'cos.

function onTouched(hit)
	local player = Players:GetPlayerFromCharacter(hit.Parent)
	if Player ~= nil then
		--// Code
	end
end

script.Parent.Touched:connect(onTouched)

You see, the :GetPlayerFromCharacter() method does, basically, what it's name implies it does. With this, it will get the Player's Player, and not the Character.

Now that you have the Player, all that's left is to award the points, and, assuming it'd called Logs like in your script example, I'll just try add it like this...

Players = game:GetService("Players") --// Gets Players

function onTouched(hit)
	local player = Players:GetPlayerFromCharacter(hit.Parent) --// Gets a Players Player from their Character
	if Player ~= nil then --// Checks if it's real.
		if player and player.leaderstats and player.leaderstats.Logs then --// Checks if the leaderstats exist (I don't even know if this works, honestly.
			player.leaderstats.Logs.Value = player.leaderstats.Logs.Value + 5 --// Awards Points
		end
	end
end

script.Parent.Touched:connect(onTouched)

Well, it should work. Looks about right...

Hope this helped. :) If it didn't please don't hate me. :(

  • Kratos232
function onTouch(hit)
	local logs = hit.Parent.leaderstats.Logs
	logs.Value = logs.Value + 5
end
script.Parent.Touched:connect(onTouch)

Try and add this to workspace.

game.Players.PlayerAdded:connect(function(plr)
	leaderstats = Instance.new("IntValue")
	leaderstats.Name = "leaderstats"

	logs = Instance.new("IntValue")
	logs.Name = "Logs"
	
	logs.Parent = leaderstats
	leaderstats.Parent = plr
end)

7 comments

0

Still isnt working...

0

It says in the output "leaderstats is not a valid member of Model" and the leaderstats are inside the Service "Player"

0

why thumbs down qq

0

How would I fix this?

0

Not sure.

0

:/

0

Updated.

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