woop de schwoop
woop de schwoop

Reputation: 107

Can't get to a value inside a player in lua

Code I Tried

game.Workspace.burgerclick.OnServerEvent:Connect(function()
    local playername = game.Workspace.ClickPlayer.Value
    local player = game.Players:FindFirstChild(playername)
    local playaname = player.Name
    local fpval = game.Players[playaname].FP.Value
    fpval = fpval + 1
end)

I am making a game and I wanted to have a button that updates the value of something called FP, in the workspace I have a stringvalue called ClickPLayer which updates to the player clicking the button. that works. I made a variable that is the value the stringvalue, but when I put the variable inside the line when I update the FP Value it says the variable is not a valid member of players. I'm not sure what to do.

Upvotes: 0

Views: 188

Answers (2)

d x
d x

Reputation: 112

The most correct and efficient code would be

local Players = game:GetService("Players")
local fpval
local plr
workspace.burgerclick.Activated:Connect(function()
    plr = workspace.ClickPlayer.Value
    fpval = Players[plr].FP
    fpval.Value = fpval.Value + 1
end)

In the documentation of TextButtons and ImageButtons, there is an Activated event that can be used for this, instead of firing a server from a LocalScript which is what your script aims to do now. However, if the button is in the PlayerGui and you have an activation LocalScript for it, you probably want to use this code instead:

local fpval
workspace.burgerclick.OnServerEvent:Connect(function(plr)
    fpval = plr.FP
    fpval.Value = fpval.Value + 1
end)

Upvotes: 0

I think the correct code should be something like:

game.Players[playaname].FP.Value = game.Players[playaname].FP.Value + 1 

Because, when you do:

 local fpval = game.Players[playaname].FP.Value
 fpval = fpval + 1

You are copying the value of game.Players[playaname].FP.Valueto fpval and incrementing the copy (fpval = fpval + 1), not the value held by game.Players[playaname].FP.

Upvotes: 1

Related Questions