Cloning from ReplicatedStorage to Backpack

I have this local script in StarterPlayerScripts:

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!”.

2 Likes

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)
1 Like

What are you actually trying to do? Because what you’re doing now is;

  • when a player joins the game it will detect if they have a keyboard
  • if they do, then whenever a new player joins the game it will give the new player a tool

If what you’re trying to do is give the player a tool when they join, you can just remove the player added and character added events

2 Likes

How then do I access the backpack to clone the tool to it? And yes I want the tool to be given to the player when they join, if they have a keyboard.

1 Like

I will try this as well, thank you.

1 Like

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.

1 Like

try printing the variables backpack or tool to see if these things are existent. If it prints nil, then that’s your issue right there.

Edit:

Or listen to what @Orbular3 said and remove the CharacterAdded in your local script.

If you can’t find the player just use

local player = game.Players.LocalPlayer
1 Like

Yes I did this

with @Orbular3 suggestion and it worked, thank you so much for your input as well!

1 Like

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.

1 Like

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)
1 Like

I tried this, but no errors and no tool in backpack.

Local script in StarterPlayerScripts too.

1 Like

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)
1 Like

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.

2 Likes

Ok it says

 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.

1 Like

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.

1 Like

This doesnt help… that being said, it was working for awhile and then stopped working. Now its not working again.

1 Like

Could I see your updated code?

1 Like

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.

1 Like

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.

1 Like

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.

2 Likes