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

Why is my variable not recognised?

Asked by 5 years ago
Edited 5 years ago
game.Players.PlayerAdded:Connect(function(plr)
    local char = plr.Character or plr.CharacterAdded:Wait()
    local hum = char.Humanoid
    local mouse = plr:GetMouse()

    mouse.KeyDown:connect(function(key)
        if key:byte() == 32 then --Since you cannot type [[key == "Space"]] just use byte.
            plr.Character.Humanoid.JumpPower = plr.Character.Humanoid.JumpPower +1
    end
    end)
end)

ServerScriptService.Script:6: attempt to index local 'mouse' (a nil value)

1 answer

Log in to vote
0
Answered by 5 years ago

Hi jalbraek! I'm here to help you!

You can't use a ServerScript to interct with the player, use a LocalScript. To make a good script, use UserInputService. Use RemoteEvent to change the JumpPower with the server.

LocalScript:

--< Services
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local UserInputService = game:GetService('UserInputService')

--< Events
UserInputService.InputBegan:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == Enum.KeyCode.Space then
        ReplicatedStorage.RemoteEvent:FireServer() -- Fire the server, send automaticly who is the client
    end
end)

Put this script in StarterGui or StarterPack or StarterPlayerScripts

Script:

--< Services
local ReplicatedStorage = game:GetService('ReplicatedStorage')

--< Events
ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player)
    player.Character.Humanoid.JumpPower = player.Character.Humanoid.JumpPower + 1
end)

Put this script in Workspace or ServerScriptService

Ad

Answer this question