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

How would I reference the player in this toolscript?

                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

✓ Accepted answer

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

1 comment

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.

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

5 comments

0

No touched is the player thats taking the dmg from the sword

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.

0

if player is holding the tool, you can use TOOL.Parent to get his character

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"

0

just change TOUCHED.Parent to TOOL.Parent

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