I get this error:
15:13:52.062 - Workspace.Detect.Main:6: attempt to index local 'Player' (a nil value) 15:13:52.063 - Stack Begin
15:13:52.063 - Script 'Workspace.Detect.Main', Line 6 15:13:52.064 - Stack End
15:13:52.064 - Disconnected event because of exception
on this script:
01 | local Enabled = true |
02 |
03 | function onClick(Brick) |
04 | if Enabled = = true then |
05 | local Player = Brick.Parent:findFirstChild( "Humanoid" ) |
06 | local Character = game:GetService( 'Players' ):GetPlayerFromCharacter(Player.Parent) |
07 | Enabled = false |
08 | print ( "Stage 1" ) |
09 | if Player ~ = nil then |
10 | print ( "Stage 2" ) |
11 | if Player.Parent.Car ~ = nil then |
12 | print ( "Stage 3" ) |
13 | if Character.Money.Value > = 50 then |
14 | print ( "Stage 4" ) |
15 | Player.Car.Fuel = player.Car.Fuel + 50 |
I've gone over it multiple times and I can't find an error. Can anyone please help?
Here's a fix:
01 | local Enabled = true |
02 |
03 | function onClick(Player) |
04 | if Enabled = = true then |
05 | Enabled = false |
06 | print ( "Stage 1" ) |
07 | if Player ~ = nil then |
08 | print ( "Stage 2" ) |
09 | if Player.Parent.Car ~ = nil then |
10 | print ( "Stage 3" ) |
11 | if Character.Money.Value > = 50 then |
12 | print ( "Stage 4" ) |
13 | Player.Car.Fuel = player.Car.Fuel + 50 |
14 | Character.Money.Value = Character.Money.Value - 50 |
15 | wait( 5 ) |
When a function is connected to the MouseClick
event, the player who clicked the part will be passed as the first argument, not any of the player's parts.
local Character = Brick.Parent:FindFirstChild("Humanoid") local Player = game.Players:GetPlayerFromCharacter(Character)
Your Character and Player variables were swapped and when you attempted to get the player(it's variable was Character) then you were using the Parent of the character for the parameters of the GetPlayerFromCharacter event.
Hope I Helped
+1
The error is Player (a nil value)
01 | function Buy(Player) |
02 | local RS = game:GetService( "ReplicatedStorage" ) |
03 | local item = RS:WaitForChild( "Ball" ) |
04 | local Price = 50 |
05 | local stats = Player.stats |
06 | if (stats.Cash.Value > = Price) then |
07 | item:Clone().Parent = Player.Backpack |
08 | item:Clone().Parent = Player.StarterGear |
09 | stats.Cash.Value = stats.Cash.Value - Price |
10 | end |
11 | end |
12 |
13 | script.Parent.MouseButton 1 Click:connect(Buy) |