Help with pick up script

I’m trying to make something fairly simple, so I feel like I’m just dumb for not getting it… It’s just a simple thing where you run into something to pick it up and it adds +1 to the leaderboard score. After everything I’ve seen, I feel like this should work, but it’s not :[

I have two separate scripts. One’s a normal script:

local LP = game:GetService("Players")

local pill = workspace:WaitForChild("pill")

local function onPlayerAdded(player)
local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"

local vScore = Instance.new("IntValue")
	vScore.Name = "Score"
	vScore.Value = 0
	vScore.Parent = leaderstats

leaderstats.Parent = player
end

for _, player in pairs(LP:GetPlayers()) do
onPlayerAdded(player)
end

LP.PlayerAdded:Connect(onPlayerAdded)

and then another that’s in a local script

local pill = workspace:WaitForChild("pill")

local function pillTouched(part)
pill.Transparency = 1
pill.CanCollide = false
game.Players.LocalPlayer.leaderstats.Score.Value=game.Players.LocalPlayer.leaderstats.Score.Value+1
end

pill.Touched:Connect(pillTouched)

I had it all in one script before, but then LocalPlayer can’t be in a normal script, and a local script doesn’t make the leaderboard, and now I’m at least not getting any errors but touching the block doesn’t do anything. Furthermore, I wanna add more blocks to pick up and I’m fairly certain there’s has to be a better way than making a million in the script lmao

I’d appreciate any help with this! Sorry if this seems like a dumb question, I couldn’t find anything that addressed this that seemed to actually help…

The reason this isn’t working, is because the Client (Player) can’t modify anything on the server. If it tries, it will be the only one seeing the changes, unless FE is disabled. To fix this you would:

local LP = game:GetService("Players")

local pill = workspace:WaitForChild("pill")

pill.Touched:Connect(function(Touch)
    if Touch.Parent then
        if LP:FindFirstChild(Touch.Parent.Name) then
            local P = LP:FindFirstChild(Touch.Parent.Name)
            if pill.Transparency == 0 then
                pill.Transparency = 1
                pill.CanCollide = false
                P.leaderstats.Score.Value = P.leaderstats.Score.Value + 1
           end
        end
    end
end)

local function onPlayerAdded(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"

    local vScore = Instance.new("IntValue")
    vScore.Name = "Score"
    vScore.Value = 0
    vScore.Parent = leaderstats

    leaderstats.Parent = player
end

for _, player in pairs(LP:GetPlayers()) do --Not sure why this is here, unless this script is enabled after X amount of players have joined
    onPlayerAdded(player)
end

LP.PlayerAdded:Connect(onPlayerAdded)

I have not been able to test this yet, so let me know if it works or not.

IT WORKS Thank you so much! I figured it had to be something with server/client communication but I’m still a babby at scripting lol. Thank youuuu!

1 Like