Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is my variable not recognised?

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

✓ Accepted answer

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

This discussion is preserved as it was. New answers, comments, and votes are closed.