✓ Accepted answer
General Practice
Use :GetService() to retrieve the Players Service
Use :WaitForChild() to make sure something exists before using or changing it
Issues
All items within the ServerStorage (as well as ServerScriptService for that matter) will not be replicated to the Client, so you cannot define the capacity value using a LocalScript directly within your current set-up
You also immediately check the Chemicals value instead of anytime it changes so if it doesn't start out as a number greater than or equal to the capacity, then it will not be added to the player's gui. It will also only move it once so if this was somehow fired again, it would be unable to perform the parenting of a now null object.
Option 1: Remote Event
Since you are using the ServerStorage to hold your BottleValue value, I would suggest using a RemoteEvent to place the Gui for the player.
Server Script under ServerScriptService
local serverstorage = game:GetService("ServerStorage")
local replicatedstorage = game:GetService("ReplicatedStorage")
local remote = Instance.new("RemoteEvent")
remote.Name = "AddSellGui"
remote.Parent = replicatedstorage
local capacity = serverstorage:WaitForChild("BottleValue")
game:GetService("Players").PlayerAdded:Connect(function(player)
local Chemicals = player:WaitForChild("leaderstats"):WaitForChild("Chemicals")
Chemicals.Changed:Connect(function()
if Chemicals.Value >= capacity.Value then
remote:FireClient(player)
end
end)
end)
Revised Local Script under StarterGui
local replicatedstorage = game:GetService("ReplicatedStorage")
local remote = replicatedstorage:WaitForChild("AddSellGui")
remote.OnClientEvent:Connect(function()
local clone = replicatedstorage:WaitForChild("Sell"):Clone()
clone.Parent = script.Parent
end)
Option 2: Moving the "capacity" value
However the easiest way to complete this task would be to simply perform everything in a LocalScript and place the BottleValue into the ReplicatedStorage
Revised LocalScript under StarterGui
local player = game:GetService("Players").LocalPlayer
local Chemicals = player:WaitForChild("leaderstats"):WaitForChild("Chemicals")
local capacity = game:GetService("ReplicatedStorage"):WaitForChild("BottleValue")
Chemicals.Changed:Connect(function()
if Chemicals.Value >= capacity.Value then
local clone = game:GetService("ReplicatedStorage"):WaitForChild("Sell"):Clone()
clone.Parent = script.Parent
end
end)
1 comment
it works so yeah