Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How can I identify the name of an object that relies on a varying property?

Asked by 4 years ago

If I make a local Instance.new and name it as a player's UserId as such;

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
    local playerid = Instance.new("NumberValue", game.ReplicatedStorage.PlayerList)
    playerid.Name = player.UserId
    playerid.Value = 0
end)

How can I address it from another script?

I tried game.ReplicatedStorage.PlayerList.(player.UserId), however, that doesn't work. I want the Instance.new to be tied to the player and I thought I would accomplish this by making the name of the object a player's UserId but by doing so I can't identify it in other scripts since it doesn't exist yet and that I don't know how to express the name of the Instance.new in another script (since player.UserId isn't working).

1 answer

Log in to vote
0
Answered by
TGazza 1336 Moderation Voter
4 years ago
Edited 4 years ago

Updated with 2 new scripts to do what you ask! :)

Code Server script:

local Players = game:GetService("Players")
local RS = game:GetService("ReplicatedStorage")
local ChageIDFunction_Name = "ChangePlayer_Value_Funtion"

Players.PlayerAdded:Connect(function(player)
    local PlayerList = (function() local F = RS:FindFirstChild("PlayerList") if(F == nil) then F = Instance.new("Folder",RS) F.Name = "PlayerList" end return F end)()
    local ChangeIDFunct = (function()
        local FunctFdr = RS:FindFirstChild("RemoteFunctions")
        if(FunctFdr == nil) then
            FunctFdr = Instance.new("Folder",RS)
            FunctFdr.Name = "RemoteFunctions"
        end
        local Funct = FunctFdr:FindFirstChild(ChageIDFunction_Name)
        if(Funct == nil) then
            Funct = Instance.new("RemoteFunction",FunctFdr)
            Funct.Name = ChageIDFunction_Name
        end
        return Funct
    end)()
    ChangeIDFunct.OnServerInvoke = (function(player,value)
        local PlayerId = player.UserId
        local Chk = PlayerList:FindFirstChild(PlayerId)
        if(Chk ~= nil) then
            Chk.Value = value
        end
    end)

    local playerid = Instance.new("NumberValue", PlayerList)
    playerid.Name = player.UserId
    playerid.Value = 0
end)

Local Script: (Note when you run this code it will continually add on 1 to the current value you have stored in your playerid value!)

Code:

local RS = game:GetService("ReplicatedStorage")
local PlayerList_Folder  = RS:WaitForChild("PlayerList",5)
local RemoteFunctions_Folder = RS:WaitForChild("RemoteFunctions",5)
local Player = game.Players.LocalPlayer

local MyId = Player.UserId
local Chk = PlayerList_Folder:WaitForChild(MyId,5)
local ChangePlayerIDValueFunct = RemoteFunctions_Folder:WaitForChild("ChangePlayer_Value_Funtion",5)

while true do

    if(Chk ~= nil) then
        print("Found IT!!! \n MyId is [",MyId,"] and the stored value is [",Chk.Value,"]" )
    else
        print("Nothings there... sleeping!")
        wait(1)
    end
    ChangePlayerIDValueFunct:InvokeServer(Chk.Value+1)
    wait()
end

Obviously you would hook up the ChangePlayerIDValueFunct:InvokeServer() inside a button or something that changed when you did an action kinda like adding to a score board etc...

Hope this helps! :)

0
I appeciate your help but sadly I get "attempt to index number with 'Value'" when I place it in my local script. ChirpPerson 74 — 4y
0
I just ran a test to see if I can change the value via a local script and I cannot, so now I have to try something else. :( Once again, thank you for your help though, I appeciate it. ChirpPerson 74 — 4y
0
Hmm.. ill see what i can come up with. Just working on an example that might work. If/when it works ill update my answer with the new code :) TGazza 1336 — 4y
0
Just updated the answer with a working script though its not tested for online use. it Should work as its using a remote function but not 100% sure" TGazza 1336 — 4y
0
@TGazza THANK YOU SO MUCH! It works perfectly! Thank you so much, I deeply appeciate your help!! :D ChirpPerson 74 — 4y
Ad

Answer this question