Hi. I’m trying to make a Shop Gui and the problem is that when you use a Local Script to get Tools it just bugs the Tool so I’m currently trying to use Server Script.
I made this Script but it still won’t work somehow:
local Players = game:GetService("Players")
local Player = Players:GetPlayers()
local Backpack = Player.Backpack
local Tool = game.ReplicatedStorage.TestFolder.Test
script.Parent.MouseButton1Click:Connect(function()
Tool:Clone().Parent = Backpack
print("Test")
end)
I found a related post of this but it’s broken for me and probably outdated.
There’s a trick I use to fetch the player from a server Script inside of a Gui:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local guiButton = script.Parent
local player = guiButton:FindFirstAncestorWhichIsA("Player")
local tool = ReplicatedStorage.TestFolder.Test
if player then
local function onMouseButton1Click()
tool:Clone().Parent = player.Backpack
print("Test")
end
guiButton.MouseButton1Click:Connect(onMouseButton1Click)
end
Since Guis will be placed inside of the player’s PlayerGui you can use FindFirstAncestorWhichIsA to fetch the player
I think you should try this updated script instead. Make sure to write the correct name of the GUI and the button in the Button variable.
local Tool = game.ReplicatedStorage.TestFolder.Test
game.Players.PlayerAdded:Connect(function(plr)
task.wait(2)
local Button = plr.PlayerGui.Gui.Button-- The name of the Gui and the button
Gui.MouseButton1Click:Connect(function()
Tool:Clone().Parent = plr
print("Test")
end)
end)
Then you get the event from server and then handle the giving tool:
Event.OnServerEvent:Connect(function(player, tool)
local backpack = player.Backpack
local tool = -- look for the tool or where you put it
tool:Clone().Parent = backpack
end)