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

?[Help] Touch part to give a specific tool

My problem

I want to make a part, if i touch it, it should give me a tool(i putted it in ReplicatedFirst). I tried to make it in FilteringEnabled, but nope. I also tried it in FilteringDisabled but not work too. Here's the code. FilteringEnabled

local Players = game:GetService("Players")
local sword = game.ReplicatedFirst.ClassicSword
local backpack = Players.LocalPlayer.Backpack

local part = script.Parent
local function onTouched(part)	
	local player = Players:GetPlayerFromCharacter(part.Parent)
	if not player then return end
	sword:Clone()
	sword.Parent = backpack
end
part.Touched:Connect(onTouched)

****FilteringDisabled

local Players = game.Players
local sword = game.ReplicatedFirst.ClassicSword
local backpack = Players.LocalPlayer.Backpack

local part = script.Parent
local function onTouched(part)	
	local player = Players:GetPlayerFromCharacter(part.Parent)
	if not player then return end
	sword:Clone()
	sword.Parent = backpack
end
part.Touched:Connect(onTouched)

Hope you guys help me. Im new in scripting in Roblox. :)

1 answer

✓ Accepted answer

You're trying to reference LocalPlayer from a server script (or at least I'd think you'd be using a server script for this), which doesn't work as it runs on the server. Try this:

local Players = game:GetService("Players")
local sword = game.ReplicatedFirst.ClassicSword

local part = script.Parent
local function onTouched(part)  
    local player = Players:GetPlayerFromCharacter(part.Parent)
    if not player then return end
    local backpack = player.Backpack
    sword:Clone().Parent = backpack
end
part.Touched:Connect(onTouched)

Also, you should probably use ReplicatedStorage or ServerStorage instead, since the sword does not need to be replicated to the client first. If you used ServerStorage, you would simply just change line 2 to local sword = game.ServerStorage.ClassicSword, and of course move the sword to ServerStorage.

1 comment

0

Thanks

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