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

Why i get this Error " sw.Mjanko25.Fireball:7: attempt to index local 'plr' (a nil value)" ?

Asked by 5 years ago

When i try to launch my Code:

--// Services \\--
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")

--// Variables \\--
local plr = game.Players.LocalPlayer
local Char = plr.Character or plr.CharacterAdded:Wait()
local Remote = ReplicatedStorage.FireBallEvent
local Mouse = plr:GetMouse()

--// Settings \\--
local Debounce = true
local Key = 'Q'

local Animation = Instance.new("Animation")
Animation.AnimationId = 'rbxassetid://128853357'

UserInputService.InputBegan:Connect(function(Input, IsTyping)
 if IsTyping then return end
 local KeyPressed = Input.KeyCode
 if KeyPressed == Enum.KeyCode[Key] and Debounce and Char then
  Debounce = false
  Remote:FireServer(Mouse.Hit)
  local LoadAnimation = Char.Humanoid:LoadAnimation(Animation)
  LoadAnimation:Play()
  wait(1)
  Debounce = true
 end
end)

I Get alwas this Error:

 sw.Mjanko25.Fireball:7: attempt to index local 'plr' (a nil value)

Can someone help me?

0
if this is a server script, it won't get player. You cant do local plr = game.Players.LocalPlayer in a server script. tonyv537 95 — 5y

1 answer

Log in to vote
2
Answered by
BenSBk 781 Moderation Voter
5 years ago
Edited 5 years ago

It's important that you understand the difference between the server and the client. Roblox uses the client-server model; when you play a game, your device, along with every other players', is a client. Every client is connected to a Roblox computer, known as the server, which manages the game and updates each client's world.

There are two distinct types of scripts in Roblox that operate either the client or the server:

It looks like your script should be LocalScript as it handles user input and attempts to get Players.LocalPlayer which is only available on the client. After all, how could the server, which manages many different clients, assign the property properly? Your issue is, however, that you are not using a LocalScript; you are using a Script. Simply switch it out and your script should work as expected.

You can learn more about the client-server model here.

Ad

Answer this question