I am working on a game where you can buy, equip, spawn then destroy the spawned house with a hammer and get money for it. Houses are stored in ReplicatedStorage until spawned, then they are located in a folder named after the player in the Workspace.
Each house is a group, for example, DefaultHouse
inside of that group is a ClickDetector and a Script and then all the blocks the house is made of…
I thought the script would run when the house is spawned but it doesn’t run at all, like, ever. It ran only when I put the house in the workspace before play testing.
I think because the house is originally in the ReplicatedStorage the script doesn’t work
Here is the script:
local house = script.Parent
local healthValue = Instance.new("IntValue")
healthValue.Name = "Health"
healthValue.Value = 100
healthValue.Parent = house
print("Script running")
-- Check if the script's parent's parent is in the Workspace
if house.Parent.Parent ~= game.Workspace then
print("Script will only run if the house is in the Workspace")
return
end
local function onHouseClicked(player)
print("House clicked:", house.Name)
-- Check if the player is holding a hammer
local character = player.Character
if not character then
print("Player character not found")
return
end
local tool = character:FindFirstChildWhichIsA("Tool")
if not tool then
print("Player is not holding a tool")
return
end
print("Tool used:", tool.Name)
-- Determine the damage based on the hammer used
local damageAmount = 0
local toolName = tool.Name
if toolName == "DefaultHammer" then
damageAmount = 10
elseif toolName == "Hammer2" then
damageAmount = 20
elseif toolName == "Hammer3" then
damageAmount = 30
end
print("Damage amount:", damageAmount)
-- Reduce the house's health
if healthValue.Value > 0 then
healthValue.Value = healthValue.Value - damageAmount
if healthValue.Value <= 0 then
-- House has been destroyed
print(house.Name .. " has been destroyed!")
house:Destroy()
else
print(house.Name .. " health reduced to " .. healthValue.Value)
end
end
end
-- Check if the house has a ClickDetector
local clickDetector = house:FindFirstChild("ClickDetector")
if clickDetector then
-- Connect the click event to the onHouseClicked function
clickDetector.MouseClick:Connect(onHouseClicked)
else
print("ClickDetector not found in house:", house.Name)
end
It is a normal script, the house is a group inside a folder called HouseFolder in ReplicatedStorage, then when spawned the house is a group inside a folder named after a player in Workspace
local player = game.Players.LocalPlayer
local playerGui = player.PlayerGui
local spawnButton = playerGui.HouseSpawner.Frame.SPAWN
local frame1 = playerGui.HouseSpawner.Frame.SPAWN.Frame1
local frame2 = playerGui.HouseSpawner.Frame.SPAWN.Frame2
local houseClone = nil
local cooldownActive = false
local TweenService = game:GetService("TweenService")
-- Function to animate the color transition of a single frame
local function animateFrame(frame, startColor, endColor, duration)
frame.BackgroundColor3 = startColor
local tweenInfo = TweenInfo.new(duration, Enum.EasingStyle.Linear)
local tween = TweenService:Create(frame, tweenInfo, {BackgroundColor3 = endColor})
tween:Play()
end
-- Function to animate the color transition of both frames simultaneously
local function animateColor(frame1, frame2, startColor1, startColor2, endColor1, endColor2, duration)
animateFrame(frame1, startColor1, endColor1, duration)
animateFrame(frame2, startColor2, endColor2, duration)
end
spawnButton.MouseButton1Click:Connect(function()
-- Check if a cooldown is active
if cooldownActive then
warn("Cooldown is active. Please wait.")
return
end
-- Check if a house is already spawned
if houseClone and houseClone:IsDescendantOf(workspace) then
warn("A house is already spawned. Destroying the existing house...")
houseClone:Destroy()
end
-- Check if the player has an island assigned
local character = player.Character
if not character then
warn("Character not found for player.")
return
end
-- Find the MainPart of the player's island
local islandMainPart = character:FindFirstChild("Island"):FindFirstChild("IslandTemplate"):FindFirstChild("HouseArea"):FindFirstChild("MainPart")
if not islandMainPart then
warn("Island's MainPart not found.")
return
end
-- Get the equipped house folder from ReplicatedStorage
local equippedHouseFolder = game.ReplicatedStorage.equippedHouse
local playerHouseFolder = equippedHouseFolder:FindFirstChild(player.Name)
if not playerHouseFolder or not playerHouseFolder:IsA("Folder") then
warn("No equipped house found for the player.")
return
end
-- Clone the player's house folder and place it on the island's MainPart
houseClone = playerHouseFolder:Clone()
houseClone.Parent = workspace
-- Find the PrimaryPart within the cloned house folder
local primaryPart = nil
local function findPrimaryPart(folder)
for _, child in ipairs(folder:GetDescendants()) do
if child.Name == "PrimaryPart" and child:IsA("BasePart") then
primaryPart = child
break
end
end
end
findPrimaryPart(houseClone)
if primaryPart then
-- Calculate the offset between the house's PrimaryPart and the island's MainPart
local offset = islandMainPart.Position - primaryPart.Position
-- Reposition the entire house by applying the offset
for _, part in ipairs(houseClone:GetDescendants()) do
if part:IsA("BasePart") then
part.Position = part.Position + offset
end
end
-- Set the PrimaryPart's CFrame to the island's MainPart position
primaryPart.CFrame = islandMainPart.CFrame
-- Start the cooldown
cooldownActive = true
-- Random cooldown duration from 5 to 10 seconds
local cooldownDuration = math.random(5, 10)
-- Animate the color transition for both Frame1 and Frame2
animateColor(frame1, frame2, Color3.fromRGB(255, 0, 0), Color3.fromRGB(255, 0, 0), Color3.fromRGB(85, 255, 127), Color3.fromRGB(75, 227, 110), cooldownDuration)
wait(cooldownDuration)
-- Reset the color of Frame1 and Frame2
frame1.BackgroundColor3 = Color3.fromRGB(85, 255, 127)
frame2.BackgroundColor3 = Color3.fromRGB(75, 227, 110)
-- Reset the cooldown
cooldownActive = false
else
warn("PrimaryPart not found or not a BasePart in the equipped house folder.")
houseClone:Destroy()
end
end)
To explain my game a bit more: every player spawns with their own island on which they can spawn the houses they bought and equipped in the store using a “SPAWN” GUI button in the corner of their screen
How would that help with the script I’m working on right now? I will try that but I don’t see how it would help the script inside the house group not working
It looks like the spawning-in script is a Localscript. The problem is, the house needs to be cloned on the server, otherwise server-scripts will not run.
Amazing
Would it be easier to just make a script for my hammer, when player clicks check for what the hammer is touching and then if it is one of the houses reduce health for that house and destroy it when the houses health reaches 0? I made something similar last year:
local tool = script.Parent
local player = script.Parent.Parent.Parent
local sound = game.Workspace.SoundBreak
game.Players.ChildAdded:connect(onPlayerEntered)
function touch(hit) --calls the function then if tool is activated check what it hit then do the following
if script.Parent.Swing.Value == true then
if hit.Name == "Smooth Block Model" then --IMPORTANT FOR THIS GAME, EVERYTHING IN "" IS DESTRUCTIBLE
hit:Destroy()
hit:breakJoints()
sound:Play()
player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 5
else
print("U cant destroy this.")
end
end
end
script.Parent.Handle.Touched:connect(touch)
this would break blocks when they touch the hammer
But wait, if I am working with a GUI that spawns those houses doesn’t it mean that the scripts have to be LocalScripts? This is the only game I ever tried making so I don’t really know much