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

How would I reference the player in this toolscript?

Asked by 4 years ago
                if TOUCHED.Parent:FindFirstChildOfClass("Humanoid") then
                    local HUM = TOUCHED.Parent:FindFirstChildOfClass("Humanoid")
                    if HUM.Health <= TOOL.Damage.Value and HUM.Health > 0 then
                        local plr = game.Players.LocalPlayer
                        plr.leaderstats.ELevel.Value = 2
                        plr.Backpack.DaggerWood:Destroy()
                        local GetDagger = game.ReplicatedStorage.DaggerIron:Clone()
                        GetDagger.Parent = plr.Backpack
                    end

This is the code I am having trouble with. I think the problem is that it can't find the localplayer in a server script, so how would I do that? A few other things to note: A kill upgrades the weapon and this is part of a toolscript. All the variables are working, its that small part that isn't. If you can help, I would greatly appreciate it. Thanks.

2 answers

Log in to vote
1
Answered by
OhManXDXD 445 Moderation Voter
4 years ago

Use GetPlayerFromCharacter on the player holding the tool.


if TOUCHED.Parent:FindFirstChildOfClass("Humanoid") then local HUM = TOUCHED.Parent:FindFirstChildOfClass("Humanoid") if HUM.Health <= TOOL.Damage.Value and HUM.Health > 0 then local plr = game:GetService("Players"):GetPlayerFromCharacter(TOOL.Parent) plr.leaderstats.ELevel.Value = 2 plr.Backpack.DaggerWood:Destroy() local GetDagger = game.ReplicatedStorage.DaggerIron:Clone() GetDagger.Parent = plr.Backpack end
0
It's not what I was looking for, though, you did answer the question I had, and I found a way around it, so I'm gonna accept this anyways. Trisodin529 89 — 4y
Ad
Log in to vote
0
Answered by
Leamir 3138 Moderation Voter Community Moderator
4 years ago

in this case, TOUCHED.Parent is the character, so you can use game.Players:GetPlayerFromCharacter(TOUCHED.Parent) to get the player

if TOUCHED.Parent:FindFirstChildOfClass("Humanoid") then
    local HUM = TOUCHED.Parent:FindFirstChildOfClass("Humanoid")
    if HUM.Health <= TOOL.Damage.Value and HUM.Health > 0 then
        local plr = game.Players:GetPlayerFromCharacter(TOUCHED.Parent)
        plr.leaderstats.ELevel.Value = 2
        plr.Backpack.DaggerWood:Destroy()
        local GetDagger = game.ReplicatedStorage.DaggerIron:Clone()
        GetDagger.Parent = plr.Backpack
    end

if this helps, please mark the answer as accepted and upvote it

0
No touched is the player thats taking the dmg from the sword Trisodin529 89 — 4y
0
I was thinking that because you're holding it you could reference the player from its hand, but that'd just get the player in workspace. Trisodin529 89 — 4y
0
if player is holding the tool, you can use TOOL.Parent to get his character Leamir 3138 — 4y
0
I tried getting the character in workspace, finding the name, then using it to reference the player, but it just says plr is not a valid member of "game.Players" Trisodin529 89 — 4y
0
just change `TOUCHED.Parent` to `TOOL.Parent` Leamir 3138 — 4y

Answer this question