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

Simple sword giver, serverstorage to player backpack?

Hi I attempted a simple sword giver script, the script isn't sending the sword to my backpack, but it will send it to the workspace if I change it, am I missing something important?

local debounce = false

game.Workspace.Part.Touched:Connect(function()
	
	if not debounce then
		
		debounce = true
		
		print("test")
		
		local cloneSword = game.ServerStorage.ClassicSword:Clone()
		
		cloneSword.Parent = game.Players:FindFirstChildOfClass("Backpack")
		
		wait(1)
		
		debounce = false
		
	end
end)

2 comments

0

Index Backpack, don't use FindFirstChild() on it.

0

As in using the i, v table thing? could you show me an example?

1 answer

✓ Accepted answer

What you need to do first is detect if the part that hit the object is actually a character. Next, you would get the character's player to get his backpack and finally clone and parent the item to the player's backpack.

Here's how you would do it:

-- Services
local ServerStorage = game:GetService("ServerStorage")
local Players = game:GetService("Players")
-- Parts
local sword = ServerStorage:FindFirstChild("ClassicSword")
-- Booleans
local debounce = false
-- Connections
game.Workspace.Part.Touched:Connect(function(hit)
	-- Check if the part that hit is the part of a character
	local char = hit.Parent
	local hum = char:FindFirstChild("Humanoid")

	if hum and not debounce then
		debounce = true
		local player = Players:GetPlayerFromCharacter(char)
		local swordClone = sword:Clone()
		swordClone.Parent = player.Backpack
		debounce = false
	end
end)

5 comments

0

Let me know how it goes!

0

Deleted my last comment, big typeo , but anything that involves the player, i will need GetService?

0

You can create a variable called Player and store all the players in that variable. You can get all players using get service or simply game.workspace.Players

0

OHH okay! that makes a lot more sense. thanks a lot!

0

You're welcome!

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