i was able to get it to change the attribute but it changes all the attributes on every model. how could i get it to just change one of the attributes?
i set line 5 and 6 to
if not v:GetAttribute(“owner”) == “nil” then
v:SetAttribute(“owner”, player.Name)
instead of
if not v:GetAttribute(“Owner”) then
v:SetAttribute(“Owner”, player.Name)
just write a break statement once you find a suitecase with owner attribute nil
Script
local Players = game:GetService("Players")
local Suitcases = workspace:WaitForChild("suitcases")
function onPlayerAdded(player)
for _, v in pairs(Suitcases:GetChildren()) do
if v:GetAttribute("owner") == nil then
v:SetAttribute("owner", player.Name)
break
end
end
end
function onPlayerRemoving(player)
for _, v in pairs(Suitcases:GetChildren()) do
if v:GetAttribute("owner") == player.Name then
v:SetAttribute("owner", nil)
break
end
end
end
Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerRemoving)
you could add a break to break the loop after you set 1 attribute
local Folder = workspace.suitcases
function onPlayerAdded(player)
for i,v in pairs(Folder:GetChildren()) do
if v:GetAttribute("Owner") and v:GetAttribute("Owner") == player.Name then
v:SetAttribute("Owner", player.Name)
break
end
end
end
function onPlayerRemoving(player)
for i,v in pairs(Folder:GetChildren()) do
if v:GetAttribute("Owner") and v:GetAttribute("Owner") == player.Name then
v:SetAttribute("Owner", nil)
break
end
end
end
game.Players.PlayerAdded:Connect(onPlayerAdded)
game.Players.PlayerAdded:Connect(onPlayerRemoving)
how would i implement a part in the script to make the parts inside the model’s transparency = 0 and cancollide true?
Loop through it’s children (or descendants if there is a child of a child) and make a statement to check if they are baseparts
function modelTransparency(model, transparency)
for _, basePart in pairs(model:GetDescendants()) do
if basePart:IsA("BasePart") then
basePart.Transparency = transparency
end
end
end
Full Script
local Players = game:GetService("Players")
local Suitcases = workspace:WaitForChild("suitcases")
function modelTransparency(model, transparency)
for _, basePart in pairs(model:GetDescendants()) do
if basePart:IsA("BasePart") then
basePart.Transparency = transparency
end
end
end
function onPlayerAdded(player)
for _, v in ipairs(Suitcases:GetChildren()) do
if v:GetAttribute("owner") == nil then
v:SetAttribute("owner", player.UserId)
modelTransparency(v, 0)
break
end
end
end
function onPlayerRemoving(player)
for _, v in ipairs(Suitcases:GetChildren()) do
if v:GetAttribute("owner") == player.UserId then
v:SetAttribute("owner", nil)
modelTransparency(v, 1)
break
end
end
end
Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerRemoving)
Also, your original error was attempting to set the Instance’s owner
attribute to an invalid attribute type. There are many available attribute types, as shown in this image:
(Common attributes types are string
, boolean
, and number
.)
When you use attributes that define a player, I recommend that you mostly use their UserId
. It’s unlikely, but you can run into a problem where the player changes their username and then rejoins the game whilst the username attribute is outdated.
You should also use the UserId
in other situations such as: Saving player data, etc…
try just setting the suit case owner to v:SetAttribute("owner", player.UserId)
When comparing use if player.UserId == v:GetAttribute("owner")
this isn’t the same as name but it is more unique. Currently you are trying to set the attribute to the player object itself, which isn’t allowed, attributes must be of a base type (number, string, boolean) or a few plain old data roblox types
And like others have said, add a break statement if owner is nil
local Players = game:GetService("Players")
local suitcases = workspace.suitcases
local function onPlayerAdded(player)
for _, v in ipairs(suitcases:GetChildren()) do
if v:GetAttribute("owner") == nil then -- do not quote "nil"
v:SetAttribute("owner", player.UserId)
break
end
end
end
Playser.PlayerAdded:Connect(onPlayerAdded)
-- also remove player ownership when they leave
local function onPlayerRemove(player)
for _, v in ipairs(suitcases:GetChildren()) do
if v:GetAttribute("owner") and v:GetAttribute("owner") == player.UserId then
v:SetAttribute("owner", nil)
break
end
end
end
Playser.PlayerRemoving:Connect(onPlayerRemove)