Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do you use the touched function and get the player in Players, not in Workspace?

Asked by 8 years ago
Edited 8 years ago
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)

2 answers

Log in to vote
1
Answered by 8 years ago

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!

0
I find it easier to check if `player` is nil, rather than looking for a humanoid. This handles more edge cases gracefully, with less code. BlueTaslem 18071 — 8y
0
I'll take a note of that because it's you of said it, lol. >3 iamnoamesa 674 — 8y
Ad
Log in to vote
-1
Answered by 8 years ago
Edited 8 years ago
plrname = part.parent


playerinplayers = game.Players:FindFirstChild("plrname")

0
Didn't work joker. ComedyPumpkin 66 — 8y
1
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. iamnoamesa 674 — 8y
0
This is wrong in a many different ways. BlueTaslem 18071 — 8y
0
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 iamnoamesa 674 — 8y

Answer this question