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

Gui Script not properly giving gear to player??? [UNANSWERED]

Asked by 10 years ago

What i am trying to do is make a game where you can choose your class and you recieve those weapons. You start off with no items but when you click the gui it will give you that specified classes weapons. here is the order i want it to be in.

1:Player clicks the gui. 2:destroy the current gear in players backpack. 3:give the player the specified gear for that class.

over a 2 week span from when i first joined this site i knew nothing about scripting but now i am learning very well from tutorials and observing scripts from free models. the gui is correctly displayed and it allows you to click it but it is not destroying the current gear nor giving that gear. here is my script.

-- these two paragraphs are just the guis
local screenGui = Instance.new("ScreenGui")
screenGui.Parent = script.Parent


local textButton = Instance.new ("TextButton")
textButton.Parent = screenGui
textButton.Position = UDim2.new(0,50,0,50)
textButton.Size = UDim2.new(0,140,0,60)
textButton.TextColor3 = BrickColor.White().Color
textButton.Style = 2
textButton.Text = "Marksman"

--once you click this it should destroy your current gear then give you the bow

textButton.MouseButton1Down:connect(function()
p = game.Players:GetPlayerFromCharacter(script.Parent)
if p == nil then return end
bow = game.ServerStorage.SnowAndArrow:clone()--Weapon name
if p.Backpack:FindFirstChild(bow.name)~= nil then return end
bow.Parent = p.Backpack
end)
0
Anyone please? ): gigabite176 20 — 10y

1 answer

Log in to vote
1
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
10 years ago

Why would you create the GUI inside the script? It would be much simpler to just create the GUI in studio then use the script to connect a click handler to it.

Your first problem is lines 17 and 18. The computer will never, ever pass line 18 because p will always equal nil.

This is rather obvious because you use the GetPlayerFromCharacter() method. As I'm sure you know, this method will take the character you gave it in the parentheses, and try to find the corresponding Player. If it's successful, it returns that Player. If it fails, it will return nil.

There is one situation, however, where this method will always return nil. This situation occurs when you don't give it a character as an argument. Think about it - how can you expect the method to find the Player of a Character unless you give it a Character?

That's exactly what you're doing here. script.Parent is going to be equal to PlayerGui, and PlayerGui is not a character. Therefore the GetPlayerFromCharacter method fails.


There are other ways to get the Player, however. If this is a LocalScript (as it should be! Always use LocalScripts when they're placed inside a Player. Although I do not think this is a LocalScript because you use ServerStorage. If this was a LocalScript you would have to use ReplicatedStorage. ) then we can just use the LocalPlayer property of Players;

print(game.Players.LocalPlayer)

Or, since the script is a descendant of Player, we can Parent up to it;

print(script.Parent.Parent) --Number of Parents needed may vary - just start counting.

Your final problem is just a typo - hopefully. Name is a capital variable, make sure it's capitalized. Objects have no name property anymore than they have a egsgs property. They have a Name property, with a capital N.

0
thank you very much!!! gigabite176 20 — 10y
Ad

Answer this question