I’m trying to achieve a script where if the player touches an object within a specific folder it would duplicate that item and store it inside the player, But instead it stores the bodypart that touched it.
I have no idea what I did wrong, Help is appreciated!
local Provider = require(game.ServerScriptService.DataProvider)
local function onTouched(part)
local player = game.Players:GetPlayerFromCharacter(part.Parent)
if player then
local data = Provider:Get(player)
if data then
local t = part:Clone()
t.Parent = player.cubesFound
print("Player"..player.UserId.."Found"..part.Name)
end
end
end
for _, v in pairs(game.Workspace.Cubes:GetChildren()) do
if v:IsA("BasePart") then
v.Touched:Connect(onTouched)
end
end
part is a reference to one of the humanoid BodyParts(basically the hit parameter passed from the Touched Event), what you should do instead is:
local Provider = require(game.ServerScriptService.DataProvider)
function onTouched(item, part)
--only use the part parameter to get the player
local player = game.Players:GetPlayerFromCharacter(part.Parent)
if player then
local data = Provider:Get(player)
if data then
local t = item:Clone() --clone item instead
t.Parent = player.cubesFound
print("Player"..player.UserId.."Found"..item.Name)
end
end
end
for _, v in pairs(workspace.Cubes:GetChildren()) do
if v:IsA("BasePart") then
v.Touched:Connect(function(hit)
OnTouched(v, hit)
end)
end
end