local sp = script.Parent
sp.Touched:connect(function(part)
local character = part.Parent
local leaderstats = character.leaderstats
local value = leaderstats.Level
if character and leaderstats and value then
value = 2
end
end)
How do you use the touched function and get the player in Players, not in Workspace?
2 answers
You can use the "GetPlayerFromCharacter" to retrieve the player through character, and the ".Character" to retrieve the character through the player. I will explain by fixing your script.
local sp = script.Parent
sp.Touched:connect(function(part)
if part.Parent:FindFirstChild("Humanoid") ~= nil then -- checks if it has humanoid
local character = part.Parent --this will get the character, not player
local player = game.Players:GetPlayerFromCharacter(character ) -- this finds the player
local leaderstats = player.leaderstats
local value = leaderstats.Level
if player and leaderstats and value then -- I'm not sure if this is entirely needed
value.Value = 2 -- You do not switch "Level" to 2, but "levels" Value, and please note that this will make it 2, not more, not less, Is this what you want?
end
end -- extra end due to checking if they have humanoid
end)
I saw multiple problems and fixed it to what I see best fit. It's a little late here now so I'm sleepy, sorry if I messed something up. Anyways, I explained what I did in the script. Have a Good Day ComedyPumpkin (or for me, night xD) -I hope I answered your question! Please accept the answer if I did :) and if not, leave a comment!
plrname = part.parent
playerinplayers = game.Players:FindFirstChild("plrname")
4 comments
Didn't work joker.
You should simply get the player from character, and not go through all that trouble of finding the player through tagging its name and finding it.. Just my preference I guess.
This is wrong in a many different ways.
1.This will find the player called plrname, not the variable. 2.This method is not what I'd recommend. 3. He had many other errors, putting that would do nothing to fix it. 4.Put local infront of plrname. 5.Put local in front of playerinplayers
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
I find it easier to check if
playeris nil, rather than looking for a humanoid. This handles more edge cases gracefully, with less code.I'll take a note of that because it's you of said it, lol. >3