I’m trying to make random parts spawn from the replicated storage to the position of spawn parts but when I do this I have to double-click on my parts. I’ve tried making the script only be clicked if mouse hover enter was functioned.
Spawning script and script firing events for client:
while true do
task.wait(5)
--Items
local item = game.ReplicatedStorage.Items
local itemtable = {
item.Cereal,
item.Beef,
item.Cake,
item.Cupcakes
}
local random = math.random(1,#itemtable)
local randomitem = itemtable[random]
--SpawnParts
local spawnparts = workspace.SpawnParts:GetChildren()
local randomSP = math.random(1,#spawnparts)
local trueSP = spawnparts[randomSP]
local newitem = randomitem:Clone()
newitem.Parent = workspace.Items
newitem.Position = trueSP.Position
newitem.ClickDetector.MouseClick:Connect(function(plr)
game.ReplicatedStorage.ClickEvent:FireClient(plr)
end)
newitem.ClickDetector.MouseHoverEnter:Connect(function(plr)
game.ReplicatedStorage.InteractionEvent2:FireClient(plr)
end)
newitem.ClickDetector.MouseHoverLeave:Connect(function(plr)
game.ReplicatedStorage.InteractionEvent3:FireClient(plr)
end)
end
Mouse hover enter, mouse hover leave, and click client functions.
game.ReplicatedStorage.ClickEvent.OnClientEvent:Connect(function()
local cs = game:GetService("CollectionService")
local player = game.Players.LocalPlayer
local money = player.leaderstats.Money
for i,v in pairs(cs:GetTagged("Item")) do
v.ClickDetector.MouseClick:Connect(function()
if v.Name == "Cereal" then
money.Value += 1
v:Destroy()
elseif v.Name == "Beef" then
money.Value += 3
v:Destroy()
elseif v.Name == "Cupcakes" then
money.Value += 2
v:Destroy()
elseif v.Name == "Cake" then
money.Value += 4
v:Destroy()
end
end)
end
end)
game.ReplicatedStorage.InteractionEvent2.OnClientEvent:Connect(function()
local cs = game:GetService("CollectionService")
for i,v in pairs(cs:GetTagged("Item")) do
v.ClickDetector.MouseHoverEnter:Connect(function()
v.Highlight.Enabled = true
end)
end
end)
game.ReplicatedStorage.InteractionEvent3.OnClientEvent:Connect(function()
local cs = game:GetService("CollectionService")
for i,v in pairs(cs:GetTagged("Item")) do
v.ClickDetector.MouseHoverLeave:Connect(function()
v.Highlight.Enabled = false
end)
end
end)
The RemoteEvents are not needed, as you can just purely do these functions in their own scripts
You’re changing values on the client, so it won’t replicate to the server. To fix this, just connect .MouseClick on the server
Spawning:
--//Services
local cs = game:GetService("CollectionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--//Variables
local item = ReplicatedStorage.Items
--//Tables
local itemtable = {
item.Cereal,
item.Beef,
item.Cake,
item.Cupcakes
}
--//Functions
local function OnItemAdded(v)
v.ClickDetector.MouseClick:Connect(function(player)
if not v.Highlight.Enabled then
return
end
local money = player.leaderstats.Money
if v.Name == "Cereal" then
money.Value += 1
v:Destroy()
elseif v.Name == "Beef" then
money.Value += 3
v:Destroy()
elseif v.Name == "Cupcakes" then
money.Value += 2
v:Destroy()
elseif v.Name == "Cake" then
money.Value += 4
v:Destroy()
end
end)
v.ClickDetector.MouseHoverEnter:Connect(function()
v.Highlight.Enabled = true
end)
v.ClickDetector.MouseHoverLeave:Connect(function()
v.Highlight.Enabled = false
end)
end
cs:GetInstanceAddedSignal("Item"):Connect(OnItemAdded)
for i, v in cs:GetTagged("Item") do
task.spawn(OnItemAdded, v)
end
while true do
task.wait(5)
--Items
local random = math.random(#itemtable)
local randomitem = itemtable[random]
--SpawnParts
local spawnparts = workspace.SpawnParts:GetChildren()
local randomSP = math.random(#spawnparts)
local trueSP = spawnparts[randomSP]
local newitem = randomitem:Clone()
newitem.Position = trueSP.Position
newitem.Parent = workspace.Items
end