see this topic of mine
thanks, I probably would have changed the duration after some major trial and error , so is there an easy way for me to identify the player?
Also that is a very nice post!
Its quite easy for any local scripts. All you have to do is:
local Player = game:GetService("Players").LocalPlayer
Local player only exist on the client, trying to define local player in a server script won’t work and will result in an error. So what you would want to do is create a RemoteEvent → when the player chats the right command, fire that remote for the client that sent the chat message → have the players client listen for that event and then post the purchase notification (the one we did above) → once the player makes a selection you then fire the same remote and this time have the server listen for that event → have the server give the desired item.
Alright I used that thanks for the reassurance!
I was trying to make the game just clone it out of the server storage folder, and just put it in the players backpack. Is that not efficient or something?
also I don’t know what happened but for some reason the server script will not let me clone the notification gui into the player’s PlayerGui… heres what the code looks like, its just your base code:
local PS = game:GetService("Players");
function PlayerJoined(Player)
Player.Chatted:Connect(function(Msg)
if string.lower(Msg) == "!buy AK-47" then
local c = script.Notif1:Clone()
c.Parent = Player.PlayerGui
end;
end);
end;
PS.PlayerAdded:Connect(PlayerJoined);
for _, Player in ipairs(PS:GetPlayers()) do
task.spawn(PlayerJoined, Player);
end;
Since we are making the message lowercase, the if statement won’t be met since AK is capital. Just change it to ak-47 instead of AK-47.
I don’t really understand – why not use a shop GUI with an open/close
button, and handle the verification of the purchase on the server? Using chat commands seems to overcomplicate this purpose and doesn’t seem very productive for a consumer, not to mention it would be easier to accomplish the former. Is there a reason for this choice in design?
It just looks cool to me. That is the entire reason.
But seriously I just do not want to make a shop since there is not downtime at all inside the game, and I just want there to be a notification since it looks cool, instead of a giant box on your screen.
That seems to have fixed it.
Also about that.
I scripted that inside of the little notification ‘if else’ statement. Did not seem to work at all (Also as of this edit I’m going to go eat)
if Reponse == "Yes" then
if plr.leaderstats.Cash.Value >= 300 then
plr.leaderstats.Cash.Value = plr.leaderstats.Cash.Value - 300
local c = SS.Weapons["AK-47"]:Clone()
c.Parent = plr.Backpack
end
else
print("Player canceled the purchase");
end;
It would be better to have an ID system of sorts and utilize a RemoteFunction
to determine whether the player purchased the weapon or not.
For example,
local Weapons = {
["AK-47"] = 47, -- Weapon name, price of weapon
}
local function RetrieveWeaponData(Arg)
for WeaponName, Price in pairs(Weapons) do
if WeaponName:lower():find(Arg) == 1 then
return WeaponName, Price
end
end
return nil, nil
end
...
local Arg = Msg:lower():match("%!buy (%w+)")
if Arg then
local WeaponName, _ = GetWeaponData(Arg)
if WeaponName then
local NewNotif = script.Notification:Clone()
NewNotif.WantedWeapon.Value = Weapon
NewNotif.TextLabel.Text = "Are you sure you want to buy [" .. Weapon .. "] ?"
NewNotif.Parent = Player.PlayerGui
end
end
...
PurchaseWeapon.OnServerInvoke = function(Player, WeaponName)
local WeaponName, Price = GetWeaponData(WeaponName)
-- Verify purchase on server, and give the specified weapon
end
That seems like a smart idea although it looks extremely confusing and
Notifications don’t work like that
That notification system seems fine to me. Simply clone the notification parent it to the player’s PlayerGui folder somewhere (so that it’s correctly rendered onto the screen) and then modify the shown GuiObjects accordingly (alter text, images etc.).
Sorry, went to bed.
The reason why its not working is because you need to give the item on the Server not the client. To do this you would have to use RemoteEvent and/or RemoteFunctions.
So right click on replicatedStorage → insert a new object (aka a remoteEvent) → name that remote whatever you want → define that remote in your notification script → have it FireServer() when player buys an item → server listens for the event → server gives item
Do I need to make a separate event for every single item?
No, what you want to do is pass arguments to differentiate between task you want to do.
E.g on the client you might have something like:
local Remote = game:GetService("ReplicatedStorage").MyRemote
Remote:FireServer("AK-47", 1235) -- we can send arguments!!
On the server script you would do:
local Remote = game:GetService("ReplicatedStorage").MyRemote
Remote.OnServerEvent:Connect(function(Player, WeaponName, RandomNumbers)
print(Player, WeaponName, RandomNumbers)
end)
I’m on mobile so there may be typos and bad formatting.
So the only arguments you really want to be passing to the server is the name of the item the player purchased. The server will then use that name to search a folder to get said item. You then can deduct whatever cash and give the item.
So your telling me I just fire the server with the item the player wants to buy, and then have a server script handle the fired event?
Yes. All the client is going to do is fire the remote and provide the name of the item. The server will handle the rest (such as finding the item, giving it, and deducting points).