so there is a part, and when i spawn it, i would like to have it only be able to be touched by the player who owns it, no one else. so far collision group is the only option, but that would mean i would make to make a collision group for each part and player. any other ideas?
Use an if statement, and check if what’s touching the part owns the part
Is this “Spawn” a UI button or is it a physical button/part in the workspace?
You could use a leaderstat and assign someone their own role when they join, like “yes” as the intvalue. As for the part:
script.Parent.Touched:Connect(function(Player)
local PlayerName = Player.Name
local plr = game.Players:FindFirstChild(PlayerName)
if plr.Leaderstat.PlayerOwnsPart.Value == "yes" then
-- script goes here
end
end)
I’m not sure if this will work because I typed it here, and I wont respond later
edit: Be sure to elaborate so people can fully understand what you want, “i would make to make a collision group for each part and player” didnt really make sense.
Hmm. Maybe collision groups aren’t the only answer here. Try this:
local players = game:GetService("Players")
local part = script.Parent
local playerOwned = part:WaitForChild("PlayerOwned") -- ObjectValue containing the player that owns the part
local function partTouched(hit)
local char = hit:FindFirstAncestorWhichIsA("Model")
if char then
local player = players:GetPlayerFromCharacter(char)
if player then
part.CanCollide = (player == playerOwned.Value)
end
end
end
part.Touched:Connect(partTouched)
i tried something like
local part = script.parent
part.touched:connect(function(touch)
if part.player.value = touch.parent.name then
script.parent.cancollide = true
else
script.parent.cancollide. = false
end
end)
i stopped using cancollide because it just falls to the void if it’s not the player, i tried anchored, it works but as long as a player that dosnt own the part touches it, the player that owns it cant move it at all, it also gets buggy after a while.
Ah. In that case, I would recommend:
- Set the part’s
CanCollide
tofalse
on the server. - On the client, set the part’s
CanCollide
totrue
.
A rather hacky solution, but it works.
You could use CollisionGroups, but what you’re looking to do is so simple and they’re so complex.
wouldn’t the ball never move on the server? can other people see it and can touched event register? there are some parts i need the part to touch
As long as CanTouch
is enabled, the .Touched
event will still fire. (it’s enabled by default)
As for the ball moving, yes you would need to anchor it which would stop it from moving. If the ball needs to move I would look into CollisionGroups again.
yeah sadly i need the part to be pushed by players, how would i go about making collision groups detection for the ball and player