Problem
I want my script to delete a part in the local characters model called part. (It is a part added to a character in Serverscriptservice). The problem is its not destroying the part
Problem 2
Same system as problem one but its with a gui. (It is a template cloned and placed from a module script). The problem is its not destroying the gui too.
I want both the part and gui to be destroyed but its not working. Please any help?
local button = script.Parent
local debounce = false
function onClick()
local h = button.Parent.Parent.Parent.Parent.Character.Humanoid
if h then
local playerName = h.Parent.Name
local player = game.Players:FindFirstChild(playerName)
if player then
local playerGui = player:WaitForChild("PlayerGui")
if playerGui then
local noti = playerGui:FindFirstChild("Notifications")
local container = noti:FindFirstChild("Container")
local temp = container:FindFirstChild("NotificationTemplate")
if noti then
if container then
if temp then
temp:Destroy()
end
end
end
local ball = h.Parent:FindFirstChild("Clone")
if ball then
h.Parent:FindFirstChild("Clone"):Destroy()
end
else
warn("Player GUI not found for player: " .. playerName)
end
else
warn("Player not found with name: " .. playerName)
end
end
end
script.Parent.MouseButton1Click:Connect(onClick)
Put your RemoteEvent in ReplicatedStorage and use this line of code to find it on your Script and LocalScript.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent") -- Change "RemoteEvent" for the name of your RemoteEvent
When the player click on the button, you detect it with the LocalScript and use FireServer on the RemoteEvent like that to send a signal to the server.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")
function onClick()
RemoteEvent:FireServer()
-- You can also destroy your Gui here or destroy it on the server it's your choice.
end
script.Parent.MouseButton1Click:Connect(onClick)
In the Script you use OnServerEvent to be able to obtain the signal sent by the clients and process the request. Know that the first argument will always be the player who sent the received signal.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")
RemoteEvent.OnServerEvent:Connect(function(player)
-- Find your part and destroy it on the server.
end)
Oh I forgot to mention something crucial that’s not mentioned at the top. This script teleports the player to another area so it has to be a script. Right?
If “teleport to another area” meant moving the character from one position to another in the workspace, then you can do it on the client side because the character will replicate automatically.
The teleporting system is in game and doesn’t transfer to any server. My game is about knocking people off a map and I want it so if you pressed a button (like in this case you get teleported to the lobby) that it destroys the part (aka a ball) and the cooldown aka the temp in the script below.
local noti = playerGui:FindFirstChild("Notifications")
local container = noti:FindFirstChild("Container")
local temp = container:FindFirstChild("NotificationTemplate")
I don’t think you can move a players position with a local script so I am using a script for that.
Yes you can teleport the client’s character with LocalScript because as I said in my last comment, the character is automatically replicated on the server from the client.
Anyway it’s a better pratice to do it on the server side. But you still can’t use MouseButton1Click with a Script. So you need to use a LocalScript to detect the click event, send a request to server from using a RemoteEventand process at the request on server side like I said in a comment above.
game.ReplicatedStorage.TeleportLobby.OnServerEvent:Connect(function(player)
local playerGui = player.PlayerGui
local temp = playerGui.Notifications.Container:FindFirstChild("NotificationTemplate")
local ball = player.Character:FindFirstChild("Clone")
if temp then
temp:Destroy()
else
print("Cant Find Temp")
end
if ball then
player.Character:FindFirstChild("Clone"):Destroy()
else
print("Cant find ball")
end
end)
The prints say both of the
print("Cant Find Temp")
print("Cant find ball")
I don’t know what’s going on because clearly it appears in the Explorer tab. Are module scripts registered on the server side?
The module script is in StarterPlayerScripts so is that issue too?
A LocalScript is a Lua code container that runs its contents on the client (player’s device).
A Script is a Lua code container that runs its contents on the server.
When you use a ModuleScript in a Script the code is executed on the server. When you use a ModuleScript in a LocalScript the code is executed on the client.
If temp is created on the client you will have to destroy it on the client.
If you create “Ball” on the client-side this one will visible only for this client.
If you create an instance on the client-side and try to send it from RemoteEvent, you will get nil on server.
If you create “Ball” on the client-side you can’t destroy it on the server-side, destroy it on the client-side.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ThrowEvent = ReplicatedStorage.ThrowEvent
local BallsToThrow = ReplicatedStorage:WaitForChild("BallsToThrow")
local gravity = 300
local throwspeed = 200
ThrowEvent.OnServerEvent:Connect(function(Player, mouse)
wait(0.28)
local ballName = Player.Index.Ball.Value
local ballToThrowTemplate = BallsToThrow:FindFirstChild(ballName)
if ballToThrowTemplate then
local ThrowItem = ballToThrowTemplate:Clone()
local offset = Vector3.new(0, 3.5, 0)
local offsettest = Vector3.new(4, 6.5, 0)
ThrowItem.CFrame = CFrame.new(Player.Character:FindFirstChild("Head").Position + offset, mouse.Position)
ThrowItem.Velocity = ThrowItem.CFrame.LookVector * throwspeed
ThrowItem.CanCollide = false
ThrowItem.Anchored = false
ThrowItem.Name = "Clone"
ThrowItem.Parent = Player.Character
local attachment = Instance.new("Attachment", ThrowItem)
local vectorforce = Instance.new("VectorForce", ThrowItem)
vectorforce.Force = Vector3.new(0, ThrowItem:GetMass() * gravity, 0)
ThrowItem:SetNetworkOwner(Player)
end
end)
This makes the part script.
Heres the script that deletes it (ServerScriptService)
game.ReplicatedStorage.TeleportLobby.OnServerEvent:Connect(function(player)
local ball = player.Character:FindFirstChild("Clone")
if ball then
ball:Destroy()
else
print("Cant find ball")
end
end)
Ball is on the server side so this script should be working, right?