local UserInputService = game:GetService("UserInputService")
if (UserInputService.KeyboardEnabled) then
wait(1)
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function()
local backpack = player:WaitForChild("Backpack")
local Tool = game.ReplicatedStorage.Light
local cloneTool = Tool:Clone()
cloneTool.Parent = player.backpack
end)
end)
print("The user's device has an available keyboard!")
else
print("The user's device does not have an available keyboard!")
end
It detects whether the player has a keyboard and if they do, clones a tool from ReplicatedStorage to their backpack, but its not working… I fear its because its in a local script, but i need the local script to detect the userinputservice.
It does print “The user’s device has an available keyboard!”.
You could try using a remote event if there’s a keyboard it will fire and write the rest of the code in a server script.
Example:
Local Script:
local sendEvent = game.ReplicatedStorage:WaitForChild("RemoteEvent") -- insert a remote event into replicated storage with that name written.
local UserInputService = game:GetService("UserInputService")
if (UserInputService.KeyboardEnabled) then
sendEvent:FireServer()
print("The user's device has an available keyboard!")
else
print("The user's device does not have an available keyboard!")
end
Server Script:
local event = game.ReplicatedStorage:WaitForChild("RemoteEvent") -- Insert remote event
event.OnServerEvent:Connect(function()
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function()
local backpack = player:WaitForChild("Backpack")
local Tool = game.ReplicatedStorage.Light
local cloneTool = Tool:Clone()
cloneTool.Parent = player.backpack
end)
end)
end)
I tried this, putting the local script in StarterPlayerScripts, the server script in ServerScriptService, the remote event and the tool in Replicated Storage… it prints but does not give me the tool.
On second thought, it makes the tool not work once in backpack. I believe the tool needs to be replicated across the server to work, so i will try your remote events instead and see if this resolves the issue.
You could also do it all in a server script, then use a remote function to find out if the player has a keyboard. (Insert a remote function into replicated storage, or change the path in the script)
-- Server script
local tool = game.ReplicatedStorage:WaitForChild("Light")
local checkPlayerKeyboard = game.ReplicatedStorage:WaitForChild("RemoteFunction")
game.Players.PlayerAdded:Connect(function(player)
local success, result = pcall(function()
return checkPlayerKeyboard:InvokeClient(player)
end)
if (success) and (result) then
local clonedTool = tool:Clone()
clonedTool.Parent = player.Backpack
end
end)
-- Local script
local UserInputService = game:GetService("UserInputService")
local checkPlayerKeyboard = game.ReplicatedStorage:WaitForChild("RemoteFunction")
checkPlayerKeyboard.OnClientInvoke = function()
return UserInputService.KeyboardEnabled
end)
Try changing the first script to this and see what it says in the output
-- Server script
local tool = game.ReplicatedStorage:WaitForChild("Light")
local checkPlayerKeyboard = game.ReplicatedStorage:WaitForChild("RemoteFunction")
game.Players.PlayerAdded:Connect(function(player)
local success, result = pcall(function()
return checkPlayerKeyboard:InvokeClient(player)
end)
assert(success, "an error occurred in the pcall")
if typeof(result) ~= "boolean" then
warn(result)
else
assert(result, "player does not have a keyboard")
end
print("Player should have recieved the tool")
if (success) and (result) then
local clonedTool = tool:Clone()
clonedTool.Parent = player.Backpack
end
end)
Ok i just went mobile for the next couple hours i will try it as soon as i get back… really appreciate your help. I know its cuz my tool has both server and local scripts in it or your first solution would have worked like a charm. For some reason your first solution renders my tool not useable.
18:36:02.056 Player should have recieved the tool - Server - Script:21
I even switched out the tool for another simpler tool, and still no tool in backpack.
EDIT: I moved the local script to StarterCharacterScripts and the new tool appears let me switch to the original tool and try.
NEW EDIT: It works with the Original tool, problem is that when you test in mobile mode, it still gives me the tool and it shouldnt… from your original method, it wasn’t giving me the tool when in mobile test.
What if you tried to unplug your keyboard as you still have a keyboard attached to your pc even though that your using the emulation feature so give it a try.
Its these two scripts from @Orbular3 above… the server script i have in ServerScriptService and the local script I have in StarterCharacterScripts… the tool and the remote function are in ReplicatedStorage.
Update: now when i test it, it works, but sometimes when i use emulator it isnt working as intended.
EDIT: ok when I test using Play and I start on the spawn point, it doesnt work as intended on mobile… when i do Play Here and i am not starting at spawn point but right next to it, it works as intended. I believe this is because the local script is in StarterCharacterScripts and not StarterPlayerScripts.
The local script part shouldn’t affect it but I would do the cloning on the server anyway due to other players not being able to see the tool when held by the player bc it’s on the client. (I’ve personally had that issue before)
Therefore I suggest doing what @GoodUsername777 said to do.
The cloning is on the server script right now. The issue I was having with @GoodUsername777 method was that it was breaking my tool… the tool has server and local scripts in it, thats why I am trying this remote function instead.