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

How do you make a Sell PopUp that tells the player to sell their leaderstats?

Asked by
Zottic 19
5 years ago

Hello, I have recently tried to make a sell pop-up, which is like what pops up in simulators when ur backpack is full, I have made a script on it, but I have failed. Here is the script:

local serverstorage = game:GetService("ServerStorage")

local replicatedstorage = game:GetService("ReplicatedStorage")

local capacity = serverstorage.BottleValue
local player = game.Players.LocalPlayer
local Chemicals = player.leaderstats.Chemicals



if Chemicals.Value >= replicatedstorage.BottleValue.Value then
wait()
replicatedstorage.Sell.Parent = player.PlayerGui
end

What im trying to do is that when the chemicals hit 10, a popup says that they need to sell their chemicals, the bottlevalue on line 6 is just the limit. However, it doesnt show the gui, can someone help me?

2 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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
it works so yeah Zottic 19 — 5y
Ad
Log in to vote
2
Answered by 5 years ago

Your code only executes once

And that is it. That is immediately when the script starts running. You should listen for the Instance.Changed event which will fire when a property changes. lua chemicals.Changed:Connect(function(newValue) --// code here end); Where newValue is the updated Value of Chemicals.

Side note

The contents of ServerStorage are inaccessible to clients. So whatever is in there will not replicate to them.

0
no i want it to execute when the chemicals hit 19 Zottic 19 — 5y
0
This answer explained what you needed to actually have your code function. With a little intuition, you should've realized that all you need to do is add your code into the Changed event. Don't forget the side note included below the answer. xPolarium 1388 — 5y

Answer this question