I am a noob to Roblox dev and am struggling to figure out detecting players despite reading lots of posts.
I am writing a script where a player pushes a block of the edge of a platform, it hits lava and transforms. It also sets a variable in the first function that I use in the second function so it detects that a player has touched it after it landed in the lava. I use that to then anchor the block so that doesn't get yeated when the player jumps on it to use it as a bridge. There may be another way to prevent the yeating but I am now dtermined to figure out why I can't detect the player touching the block as it is a fairly fundamental skill.
The script is located within the same folder as the block parts within the Workspace.
local Part1 = workspace.BaseLava
local Part2 = workspace.PushableBoxes.PushableBox
-- handle the touch event
local function onPart2Touched(otherPart)
-- check if the touching part is Part1
if otherPart == Part1 then
Part2.Material = "Slate"
Part2.Transparency = 0
blockPushed = 1
print(blockPushed)
end
end
Part2.Touched:Connect(onPart2Touched)
local Player = game:GetService("Players").LocalPlayer
-- handle the touch event
local function onPart2TouchedAgain(otherPart)
-- Check if block previously touched the lava
if blockPushed == 1 then
-- Check if the player touched the block
if otherPart == Player then
Part2.Anchored = true
Part2.Material = "Brick" -- Just to make it easy to see, delete when done
end
end
end
Part2.Touched:Connect(onPart2TouchedAgain)
You can use GetPlayerFromCharacter then make the blockPushed accessable to all of it.
It should look something like this:
local Players = game:GetService("Players")
local Part1 = workspace.BaseLava
local Part2 = workspace.PushableBoxes.PushableBox
local blockPushed = false
-- When the box touches the lava
local function onPart2Touched(otherPart)
if otherPart == Part1 then
Part2.Material = Enum.Material.Slate
Part2.Transparency = 0
blockPushed = true
print("Block touched lava")
end
end
-- When something touches the box again
local function onPart2TouchedAgain(otherPart)
if blockPushed then
local character = otherPart:FindFirstAncestorOfClass("Model")
local player = Players:GetPlayerFromCharacter(character)
if player then
print(player.Name .. " touched the block")
Part2.Anchored = true
Part2.Material = Enum.Material.Brick
end
end
end
Part2.Touched:Connect(onPart2Touched)
Part2.Touched:Connect(onPart2TouchedAgain)