How would i go about this?

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?

7 Likes

Use an if statement, and check if what’s touching the part owns the part

3 Likes

Is this “Spawn” a UI button or is it a physical button/part in the workspace?

2 Likes

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.

4 Likes

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)
6 Likes

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.

2 Likes

Ah. In that case, I would recommend:

  1. Set the part’s CanCollide to false on the server.
  2. On the client, set the part’s CanCollide to true.

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.

2 Likes

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

2 Likes

As long as CanTouch is enabled, the .Touched event will still fire. (it’s enabled by default)

image

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.

1 Like

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

1 Like