So i want to make a sell part the script looks like this :
script.Parent.Touched:Connect(function(hit) if hit.parent:FindFirstChild("Humanoid") then local plr = game.Players.LocalPlayer local Cash = plr.leaderstats.Cash local Stren = plr.leaderstats.Strength Cash.Value = Cash.Value + Stren.Value Stren.Value = 0 end end)
Why a have this error ?
Ok, So Here The Problem
local plr = game.Players.LocalPlayer
(Line 3).
To search LocalPlayer
, you cant use Script
. You must use LocalScript
! BUT, if you still want to use your Script
you can use this function
:
GetPlayerFromCharacter
This function returns the Player
associated with the given Player.Character
, or nil
if one cannot be found.
script.Parent.Touched:Connect(function(hit) if hit.parent:FindFirstChild("Humanoid") then local plr = game.Players:GetPlayerFromCharacter(hit.Parent) --Just Change This local Cash = plr.leaderstats.Cash local Stren = plr.leaderstats.Strength Cash.Value = Cash.Value + Stren.Value Stren.Value = 0 end end)
Hope It Helps!
Is it a local script? You can only access the LocalPlayer from a local script
https://developer.roblox.com/en-us/api-reference/property/Players/LocalPlayer