Finding the local player

I haven’t used roblox studio in forever due to focusing on python programming. I had an idea for a game but I can’t remember how to find the local player.

This is a code snippet from a sword giver (not working):

sword = game.ReplicatedStorage.ClassicSword
Players = game:GetService("Players")
player = game.Players.LocalPlayer
print(player)

script.Parent.Touched:Connect(function(object)
	if object.Parent:FindFirstChild('Humanoid') then
		local giveSword = sword:Clone()
		giveSword.Parent = player.Backpack
	end
end)

LocalScripts run on the players’ individual computers and as such, LocalScripts have a LocalPlayer. Regular scripts run on the server, and so they don’t. If this is a LocalScript, it probably doesn’t run because it’s in Workspace. What you need to do is make this a Script and instead of using LocalPlayer, use this.

script.Parent.Touched:Connect(function(object)
    local player = game.Players:GetPlayerFromCharacter(object.Parent)
    if player then
        sword:Clone().Parent = player.Backpack
    end
end)
2 Likes