Reputation: 37
I'm trying to script a Roblox game. The aim for now is when I click the "AttractButton" the Object should attract towards player's location and stop 4 studs away. On the other hand, clicking the "RepelButton" should repel the Object a certain distance away. That works fine. the Object collides with another part that's called Orb and stops in its tracks. However, I'm trying to make the Object disappear by the destroy() function but it's just not working no matter how much debugging I do. Can someone help me?
This script is in the StarterGui:
local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")
local CollectionService = game:GetService("CollectionService")
local RunService = game:GetService("RunService")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local controlGui = player:WaitForChild("PlayerGui"):WaitForChild("ControlGui")
local attractButton = controlGui:WaitForChild("AttractButton")
local repelButton = controlGui:WaitForChild("RepelButton")
local attractMode = false
local repelMode = false
local cooldownTime = 2
local lastInteractionTime = {}
local velocity = 10 -- studs per second
-- Function to handle the attract button click
local function onAttractButtonClick()
attractMode = true
repelMode = false
print("Attract mode enabled")
-- Visual feedback for button click
attractButton.BackgroundColor3 = Color3.fromRGB(200, 200, 200) -- Change to a different color
wait(0.1) -- Wait for a short delay
attractButton.BackgroundColor3 = Color3.fromRGB(255, 255, 255) -- Revert to original color
end
-- Function to handle the repel button click
local function onRepelButtonClick()
attractMode = false
repelMode = true
print("Repel mode enabled")
-- Visual feedback for button click
repelButton.BackgroundColor3 = Color3.fromRGB(200, 200, 200) -- Change to a different color
wait(0.1) -- Wait for a short delay
repelButton.BackgroundColor3 = Color3.fromRGB(255, 255, 255) -- Revert to original color
end
attractButton.MouseButton1Click:Connect(onAttractButtonClick)
repelButton.MouseButton1Click:Connect(onRepelButtonClick)
-- Function to perform raycast check
local function raycastCheck(startPos, endPos)
local direction = (endPos - startPos).Unit * (endPos - startPos).Magnitude
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.FilterDescendantsInstances = {player.Character}
local result = workspace:Raycast(startPos, direction, raycastParams)
return result
end
-- Function to move object to final position with collision checking
local function moveObject(target, finalPosition)
local connection
local function onHeartbeat()
local currentPosition = target.Position
local direction = (finalPosition - currentPosition).Unit
local deltaTime = RunService.Heartbeat:Wait()
local newPosition = currentPosition + direction * velocity * deltaTime
local result = raycastCheck(currentPosition, newPosition)
if result then
target.Position = result.Position - direction * 1 -- Stop just before the obstacle
connection:Disconnect()
return
elseif (currentPosition - finalPosition).Magnitude < 1 then
target.Position = finalPosition
connection:Disconnect()
return
end
target.Position = newPosition
end
connection = RunService.Heartbeat:Connect(onHeartbeat)
end
-- Function to handle mouse button click
mouse.Button1Down:Connect(function()
local target = mouse.Target
if target and target:IsA("BasePart") and CollectionService:HasTag(target, "attractable") then
local currentTime = tick()
local lastTime = lastInteractionTime[target]
if lastTime and (currentTime - lastTime) < cooldownTime then
print("Cooldown active. Please wait.")
return
end
lastInteractionTime[target] = currentTime
local character = player.Character or player.CharacterAdded:Wait()
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local targetPosition = humanoidRootPart.Position
local offset, finalPosition
if attractMode then
offset = (target.Position - targetPosition).Unit * 4
finalPosition = targetPosition + offset
elseif repelMode then
offset = (target.Position - targetPosition).Unit * 20
finalPosition = target.Position + offset
end
finalPosition = Vector3.new(finalPosition.X, target.Position.Y, finalPosition.Z) -- Keep Y the same
moveObject(target, finalPosition)
print(attractMode and "Object attracted" or "Object repelled")
end
end)
This is the code in Object:
local object = script.Parent -- Assuming the script is directly inside the Object part
local orb = game.Workspace:WaitForChild("Orb") -- Assuming the orb is named "Orb" and is a separate part in the Workspace
local function onHit(otherPart)
if otherPart == orb then
object:Destroy()
end
end
object.Touched:Connect(onHit)
Upvotes: 0
Views: 233
Reputation: 43
In your code, you compared the object to an undefined parameter, which will return false. First check if otherpart
is a part, then check if it has the same name as the orb
, and then destroy object
.
Use a basic system like this:
--server script
local function onHit(otherpart)
if otherpart:IsA("BasePart") then
if otherpart.Name==orb.Name then
object:Destroy()
Upvotes: 0