Cloning from ReplicatedStorage to Backpack

Ah I see

1 Like

Ok the final result here is that sometimes it works and sometimes it doesnt, from what i gather and researched is that sometimes the player has spawned already and the cloning isn’t happening because it is waiting for the player to be added.

Sometimes when the player spawns too in mobile emulation, it thinks the player has a keyboard and prints a success, and clones the tool as well.

I tried adding wait () and i tried to change player.Backpack to player:WaitforChild(ā€œBackpackā€) and neither one consistently worked.

I also tried adding a CharacterAdded function after the PlayerAdded one but this didn’t resolve.

I know the issue is how the player and character gets added but not sure how to resolve.

1 Like

Maybe instead of only using KeyboardEnabled you can try TouchEnabled to help you out. Play with it, and see where it gets you to. You can check the article for more info.

2 Likes

How would i incorporate the TouchEnabled to @Orbular3 scripts so that I check for a KeyboardEnabled and TouchEnabled together. Keep in mind I need it to return a success on the Keyboard and a false on the TouchEnabled.

1 Like

Change this line

return UserInputService.KeyboardEnabled

to this

return not UserInputService.TouchEnabled
1 Like

I seem to have fixed the issue by adding a wait and changing the (result) to (UserInputService.KeyboardEnabled) like this:

	wait(3)
	if (success) and (UserInputService.KeyboardEnabled) then
		local clonedTool = tool:Clone()
		clonedTool.Parent = player.Backpack
	end

Question now is this ok? can i switch out the (result) for this? Not sure why this has been working perfectly so far? not that familiar with the pcall and the way it was setup

1 Like

And yes this also works. . . .

Thank you both @Orbular3 and @GoodUsername777 for all of your help!!!

Since I used mainly your scripts @Orbular3 I will link you for solution, but thank you both again.

2 Likes

This shouldn’t work because you’re checking it on the server, instead of the client

1 Like

That was my thought!! but since it was working i thought since it was in () and it was from the pcall that it was pulling the result from the remote function??

Edit: i retested the old way and still gives me tool on mobile… i dont know how the UIS is working from a server script!!

1 Like

No, what pcall does is it doesn’t error but instead makes the variable success false and gives the string error in the variable result

1 Like

in that case what does the (success) and (result) together give you?

If success is true, what would result be?

1 Like

Success is the Bool value (true or false) based on whether the code ran without issue or not and result is the error

1 Like

I’m reading that since we have UIS linked up though a remote function or event, you can now call on it to do server sided functions.

1 Like

If success is false, then result is the error message. If success is true, then result is what was returned inside the pcall

2 Likes

in mobile emulation, it might think the player has a keyboard because when you press any key in the mobile emulator it changes ur input to keyboard, so it would be best to test this on a real device

1 Like

I tested this by disabling my bluetooth keyboard before testing, but you are right the best thing is to test in real life.

1 Like

One more question: when I reset the character, it spawns without the tool… how can i adjust the script to account for this?

I tried to place the local script in startercharacterscripts, but this made it revert to how it was acting before.

1 Like

Add a CharacterAdded event so that the script runs whenever the character spawns in

-- Server script

local tool = game.ReplicatedStorage:WaitForChild("Light")
local checkPlayerKeyboard = game.ReplicatedStorage:WaitForChild("RemoteFunction")


game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function()
        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)
end)
2 Likes

Awesome uve been a real help and ive learned a lot…i appreciate it!

1 Like

It returns an error: ā€œan error occurred in the pcallā€ :slight_smile:

local tool = game.ReplicatedStorage:WaitForChild("Light")
local checkPlayerKeyboard = game.ReplicatedStorage:WaitForChild("RemoteFunction")
local UserInputService = game:GetService("UserInputService")

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function()
	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 received the tool")
	
	wait(3)
	if (success) and UserInputService.KeyboardEnabled then
		local clonedTool = tool:Clone()
		clonedTool.Parent = player.Backpack
	end
	end)
	end)

Is it because it is checking for player and returning a character instead?

1 Like