Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

?[Help] Touch part to give a specific tool

Asked by
c0nc3rt 61
5 years ago

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

01local Players = game:GetService("Players")
02local sword = game.ReplicatedFirst.ClassicSword
03local backpack = Players.LocalPlayer.Backpack
04 
05local part = script.Parent
06local function onTouched(part) 
07    local player = Players:GetPlayerFromCharacter(part.Parent)
08    if not player then return end
09    sword:Clone()
10    sword.Parent = backpack
11end
12part.Touched:Connect(onTouched)

****FilteringDisabled

01local Players = game.Players
02local sword = game.ReplicatedFirst.ClassicSword
03local backpack = Players.LocalPlayer.Backpack
04 
05local part = script.Parent
06local function onTouched(part) 
07    local player = Players:GetPlayerFromCharacter(part.Parent)
08    if not player then return end
09    sword:Clone()
10    sword.Parent = backpack
11end
12part.Touched:Connect(onTouched)

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

1 answer

Log in to vote
0
Answered by 5 years ago

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:

01local Players = game:GetService("Players")
02local sword = game.ReplicatedFirst.ClassicSword
03 
04local part = script.Parent
05local 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
10end
11part.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.

0
Thanks c0nc3rt 61 — 5y
Ad

Answer this question