It's because MouseButton1Click
doesn't have a parameter, meaning x
does not exist and will never be the player that clicked the GUI Button.
To get the player that clicked the button, it's best to switch from normal script to local script then use Players.LocalPlayer
.
But wait, there's a problem! You can't access ServerStorage through a local script. You can use ReplicatedStorage to clone your weapon to the player. This is how you would do it.
1 | local ak = game.ReplicatedStorage:WaitForChild( "Ak47" ) |
2 | script.Parent.MouseButton 1 Click:Connect( function () |
3 | local clone = ak:Clone() |
4 | clone.Parent = game.Players.LocalPlayer.Backpack |
But you will notice a problem, you can't damage players with your weapon.
This is how you fix that:
Server script(not local script), place this in ServerScriptService
3 | local ak = game.ReplicatedStorage:WaitForChild( "Ak47" ) |
4 | game.ReplicatedStorage:WaitForChild( "RemoteEvent" ).OnServerEvent:Connect( function (plr) |
5 | local clone = ak:Clone() |
6 | clone.Parent = plr.Backpack |
Local Script, place this inside your button
1 | script.Parent.MouseButton 1 Click:Connect( function () |
2 | for i,v in game.Players.LocalPlayer.Backpack:GetChildren() do |
3 | if v.Name = = "Ak47" then return end |
5 | game.ReplicatedStorage:WaitForChild( "RemoteEvent" ):FireServer() |