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
01 | local Players = game:GetService( "Players" ) |
02 | local sword = game.ReplicatedFirst.ClassicSword |
03 | local backpack = Players.LocalPlayer.Backpack |
04 |
05 | local part = script.Parent |
06 | local function onTouched(part) |
07 | local player = Players:GetPlayerFromCharacter(part.Parent) |
08 | if not player then return end |
09 | sword:Clone() |
10 | sword.Parent = backpack |
11 | end |
12 | part.Touched:Connect(onTouched) |
****FilteringDisabled
01 | local Players = game.Players |
02 | local sword = game.ReplicatedFirst.ClassicSword |
03 | local backpack = Players.LocalPlayer.Backpack |
04 |
05 | local part = script.Parent |
06 | local function onTouched(part) |
07 | local player = Players:GetPlayerFromCharacter(part.Parent) |
08 | if not player then return end |
09 | sword:Clone() |
10 | sword.Parent = backpack |
11 | end |
12 | part.Touched:Connect(onTouched) |
Hope you guys help me. Im new in scripting in Roblox. :)
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:
01 | local Players = game:GetService( "Players" ) |
02 | local sword = game.ReplicatedFirst.ClassicSword |
03 |
04 | local part = script.Parent |
05 | local function onTouched(part) |
06 | local player = Players:GetPlayerFromCharacter(part.Parent) |
07 | if not player then return end |
08 | local backpack = player.Backpack |
09 | sword:Clone().Parent = backpack |
10 | end |
11 | 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.