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

Remove a part only on clients screen (easy solution / fix) [?]

Asked by 4 years ago

So im kinda confused how to do this it shouldnt be very hard tho.

When you touch a part it will get destroyed, but how do I make it so it only gets destroyed on clients screen?

function onTouch(part) 
    local humanoid = part.Parent:FindFirstChild("Humanoid") 
    if (humanoid ~= nil) then   
        script.Parent:Destroy()
    end 
end

script.Parent.Touched:connect(onTouch)

2 answers

Log in to vote
0
Answered by
Fifkee 2017 Community Moderator Moderation Voter
4 years ago
Edited 4 years ago

Use a RemoteFunction that tells the client that touched it to destroy it on their screen.

To do this, you should reference part.Parent and call GetPlayerFromCharacter from the part's parent. if GetPlayerFromCharacter returns a Player Object, then you can invoke the RemoteFunction with the first argument as the player, and the second argument as the part.

Now, in a localscript, you can handle the RemoteEvent.OnClientInvoke by checking if the PlayerObject name matches their's on the client. If it matches, then delete the part (second argument)

By the way, OnClientInvoke is not a signal. It requires a function.

RemoteEvent.OnClientInvoke = function(arguments_here)
end
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

ServerScript:

local ReplicatedStorage = game:GetService("ReplicatedStorage") -- This is just where I am going to store the remote event.

local players = game:GetService("Players")

local destroyEvent = Instance.new("RemoteEvent", ReplicatedStorage)
destroyEvent.Name = "destroyEvent"

local part = script.Parent

part.Touched:connect(function(hit)
    local hum = hit.Parent:FindFirstChild("Humanoid")
    if hum ~= nil then
        for _, v in pairs(players:GetPlayers()) do
            if v.Character = hit.Parent then
                destroyEvent:FireClient(v, part)
            end
        end
    end
end)


LocalScript:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local destroyEvent= replicatedStorage:WaitForChild("destroyEvent")

destroyEvent.OnClientEvent:connect(function(part)
    part:Destroy()
end)

Something like this should do the trick, hope this helped.

Answer this question