Hello fellow developers!
As I am working with my current project, I have encountered a bug where 1 action commits to all (What I mean about that is if I like my post, all posts are affected (liked also)). The problem resides here, any help would be appreciated! Thanks!
CLIENT:
-- Services
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
-- Function to handle interactions for a specific post
local function handlePostInteractions(post)
local HeartReact = post:FindFirstChild("Heart")
local HeartImage = post:FindFirstChild("HeartImage")
local LikeCountText = post:FindFirstChild("LikeCount")
local NotificationTemplate = Players.LocalPlayer.PlayerGui.Interface.MainFrame.Sections.Notification.Notifications:FindFirstChild("NotificationTemplate")
local NotifificationsSF = Players.LocalPlayer.PlayerGui.Interface.MainFrame.Sections.Notification:FindFirstChild("Notifications")
if not HeartReact or not HeartImage or not LikeCountText or not NotificationTemplate or not NotifificationsSF then
warn("One or more required elements are missing in post")
return
end
local tweenInfo = TweenInfo.new(0.2, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut)
local tweenShow = TweenService:Create(HeartImage, tweenInfo, {Size = UDim2.new(0, 45, 0, 45), ImageTransparency = 0})
local tweenHide = TweenService:Create(HeartImage, tweenInfo, {Size = UDim2.new(0, 0, 0, 0), ImageTransparency = 1})
local lastTapTime = 0
local doubleTapThreshold = 0.3
-- Initialize unique post state
local isLiked = false
local likeCount = 0
local NotificationType = {
Like_Notification = " liked your post. Check it out!",
Add_Friend_Notification = " requested to be friends with you",
Warning_Player = "Our team has detected that your behavior does not follow the Roblox TOS. You have been warned"
}
local function updateLikeCount()
if LikeCountText then
LikeCountText.Text = tostring(likeCount)
else
warn("LikeCountText is missing in post")
end
end
local function Notify(notificationType, player)
local newNotification = NotificationTemplate:Clone()
newNotification.Parent = NotifificationsSF
newNotification.Visible = true
if notificationType == "Like_Notification" then
newNotification.Content.Text = player.Name .. NotificationType.Like_Notification
elseif notificationType == "Add_Friend_Notification" then
newNotification.Content.Text = player.Name .. NotificationType.Add_Friend_Notification
elseif notificationType == "Warning_Player" then
newNotification.Content.Text = NotificationType.Warning_Player
else
warn("Unknown notification type: " .. tostring(notificationType))
end
end
local function onHeartReactDoubleTap()
isLiked = not isLiked
if isLiked then
likeCount = likeCount + 1
Notify("Like_Notification", Players.LocalPlayer)
else
likeCount = likeCount - 1
end
updateLikeCount()
HeartImage.Visible = true
tweenShow:Play()
tweenShow.Completed:Wait()
wait(0.5)
tweenHide:Play()
tweenHide.Completed:Wait()
HeartImage.Visible = false
end
UserInputService.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
local currentTime = tick()
if currentTime - lastTapTime <= doubleTapThreshold then
onHeartReactDoubleTap()
end
lastTapTime = currentTime
end
end)
end
-- Function to create a post
local function createPost()
local PostTemplate = Players.LocalPlayer.PlayerGui.Interface.MainFrame.Sections.Home.Feed:FindFirstChild("PostTemplate")
if not PostTemplate then
warn("PostTemplate is nil")
return
end
local Feed = Players.LocalPlayer.PlayerGui.Interface.MainFrame.Sections.Home.Feed
local newPost = PostTemplate:Clone()
newPost.Parent = Feed
-- Set up the post
local profileImage = newPost:FindFirstChild("Profile")
if profileImage then
profileImage.Image = game:GetService("Players"):GetUserThumbnailAsync(game:GetService("Players").LocalPlayer.UserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
else
warn("Profile is missing in newPost")
end
local playerName = newPost:FindFirstChild("PlayerName")
if playerName then
playerName.Text = game.Players.LocalPlayer.Name
else
warn("PlayerName is missing in newPost")
end
local contentText = newPost:FindFirstChild("Content")
if contentText then
contentText.Text = InputBox.Text
else
warn("Content is missing in newPost")
end
local creationTime = os.time()
newPost:SetAttribute("CreationTime", creationTime)
local function updateTimeAgo()
local elapsedTime = os.time() - creationTime
local seconds = elapsedTime
local minutes = math.floor(seconds / 60)
local hours = math.floor(minutes / 60)
local days = math.floor(hours / 24)
local timeAgoText
if days > 0 then
timeAgoText = days .. " day(s) ago"
elseif hours > 0 then
timeAgoText = hours .. " hour(s) ago"
elseif minutes > 0 then
timeAgoText = minutes .. " minute(s) ago"
else
timeAgoText = seconds .. " second(s) ago"
end
local timeAgoLabel = newPost:FindFirstChild("TimeAgo")
if timeAgoLabel then
timeAgoLabel.Text = timeAgoText
else
warn("TimeAgo is missing in newPost")
end
end
updateTimeAgo()
InputBox.Text = ""
newPost.Visible = true
handlePostInteractions(newPost)
end
MODULESCRIPT INSIDE REPLICATEDSTORAGE
local module = {}
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
function module.CreatePostHandler(newPost)
local HeartReact = newPost:FindFirstChild("Heart")
local HeartImage = newPost:FindFirstChild("HeartImage")
local LikeCountText = newPost:FindFirstChild("LikeCount")
local NotificationTemplate = Players.LocalPlayer.PlayerGui.Interface.MainFrame.Sections.Notification.Notifications:FindFirstChild("NotificationTemplate")
local NotifificationsSF = Players.LocalPlayer.PlayerGui.Interface.MainFrame.Sections.Notification:FindFirstChild("Notifications")
if not HeartReact or not HeartImage or not LikeCountText or not NotificationTemplate or not NotifificationsSF then
warn("One or more required elements are missing in newPost")
return
end
local tweenInfo = TweenInfo.new(0.2, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut)
local tweenShow = TweenService:Create(HeartImage, tweenInfo, {Size = UDim2.new(0, 45, 0, 45), ImageTransparency = 0})
local tweenHide = TweenService:Create(HeartImage, tweenInfo, {Size = UDim2.new(0, 0, 0, 0), ImageTransparency = 1})
local lastTapTime = 0
local doubleTapThreshold = 0.3
local isLiked = false
local likeCount = 0
local NotificationType = {
Like_Notification = " liked your post. Check it out!",
Add_Friend_Notification = " requested to be friends with you",
Warning_Player = "Our team has detected that your behavior does not follow the Roblox TOS. You have been warned"
}
local function updateLikeCount()
if LikeCountText then
LikeCountText.Text = tostring(likeCount)
else
warn("LikeCountText is missing in newPost")
end
end
local function Notify(notificationType, player)
local newNotification = NotificationTemplate:Clone()
newNotification.Parent = NotifificationsSF
newNotification.Visible = true
if notificationType == "Like_Notification" then
newNotification.Content.Text = player.Name .. NotificationType.Like_Notification
elseif notificationType == "Add_Friend_Notification" then
newNotification.Content.Text = player.Name .. NotificationType.Add_Friend_Notification
elseif notificationType == "Warning_Player" then
newNotification.Content.Text = NotificationType.Warning_Player
else
warn("Unknown notification type: " .. tostring(notificationType))
end
end
local function onHeartReactDoubleTap()
isLiked = not isLiked
if isLiked then
likeCount = likeCount + 1
Notify("Like_Notification", Players.LocalPlayer)
else
likeCount = likeCount - 1
end
updateLikeCount()
HeartImage.Visible = true
tweenShow:Play()
tweenShow.Completed:Wait()
wait(0.5)
tweenHide:Play()
tweenHide.Completed:Wait()
HeartImage.Visible = false
end
UserInputService.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
local currentTime = tick()
if currentTime - lastTapTime <= doubleTapThreshold then
onHeartReactDoubleTap()
end
lastTapTime = currentTime
end
end)
end
return module
Video:
robloxapp-20240723-2302230.wmv (153.5 KB)