Search code examples
luaroblox

My Roblox script only works for the first player that joins the game


This script is run from within the workspace and should update the local GUI value for the local player once they interact with the Proximity Prompt (prox)

player = game:GetService("Players").PlayerAdded:Wait()
repeat wait() until player.Character
local repStore = game.ReplicatedStorage
local remoteEvent = repStore["Remote Events"]:WaitForChild("avatarView")
local values = player.Values
local prox = script.Parent.viewAvatar

prox.Triggered:Connect(function(player)
    values["Total Price"].Value = "R15 MAN"
    remoteEvent:FireClient(player)
end)

The script works flawlessly, however only for the first person that joins the game, for every player afterwards they will get the default values and it wont update them like it should in the script.

I will note that the values are cloned from ReplicatedStorage and placed inside the local player, but this is not an issue as I have done a team test and each player gets the cloned values, but only the first players' values update.

The following is the code which responds to the remoteEvent in the first script. I cant imagine the fault is with this code as the final line which opens the shop's GUI still functions. However i felt it was useful to include this just incase.

local player = game.Players.LocalPlayer
repeat wait() until player.Character
local repStore = game.ReplicatedStorage
local remoteEvent = repStore["Remote Events"]:WaitForChild("avatarView")
local shop = script.Parent.Main
local values = player.Values
local totalPrice = values["Total Price"]

remoteEvent.OnClientEvent:Connect(function()
    shop.Price.Text = totalPrice.Value
    wait(0.2)
    shop.Visible = true
end)

I would like to say that I have tried this inside Roblox Studio, No errors appear. I have tried this as a normal Roblox experience and it still does not work.

I have tried changing the lines of code which wait for the player to load in the game as I believe this is where the fault is. However I am unable to get it to work, still.


Solution

  • Instead of doing this.

    values["Total Price"].Value = "R15 MAN"
    
    

    You can do something like this. Since Values is only for the first player to join the server. Hope this helps.

    player.Values["Total Price"].Value = "R15 MAN"