What do you want to achieve?
I want to create a water splash system !
What is the issue?
My code is not working properly !
What solutions have you tried so far?
I did this but I don’t think this is the best way to do it :
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local RootPart = Character:WaitForChild("HumanoidRootPart")
local RunService = game:GetService("RunService")
local TouchWater = false
RunService.Heartbeat:Connect(function()
local BodyPos = Player.Character.HumanoidRootPart.CFrame
local x, y, z = BodyPos.X, BodyPos.Y, BodyPos.Z
local min, max = Vector3.new(x + 0.01, y + 0.01, z + 0.01), Vector3.new(x - 0.01, y - 0.01, z - 0.01)
local region = Region3.new(max, min)
region = region:ExpandToGrid(4)
if region then
local material = workspace.Terrain:ReadVoxels(region, 4)
if material[1][1][1] == Enum.Material.Water then
TouchWater = true
else
TouchWater = false
end
end
end)
Humanoid.StateChanged:Connect(function(oldState, newState)
if newState == Enum.HumanoidStateType.Landed or oldState == Enum.HumanoidStateType.Freefall and newState == Enum.HumanoidStateType.Swimming or oldState == Enum.HumanoidStateType.Running and newState == Enum.HumanoidStateType.Swimming then
if TouchWater == true then
workspace.Part.ParticleEmitter.Enabled = true
wait(1)
workspace.Part.ParticleEmitter.Enabled = false
end
end
end)
while wait() do
workspace.Part.CFrame = Player.Character.HumanoidRootPart.CFrame
end
‘My code isn’t working’ doesn’t tell us what exactly is happening.
Does the splash never happen? Happen late? Only happen in Studio but not in-game? Do you get any errors in the Output window?
When you have issues you should troubleshoot by putting print statements in each section of code to see where the problem is happening.
Why not put the ParticleEmitter directly in the player instead of dragging a Part along with them?
I’ve do this but it doesn’t work (the particle emmiter is set to enabled = false or true but we can’t see the particles!) :
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local RootPart = Character:WaitForChild("HumanoidRootPart")
local RunService = game:GetService("RunService")
local TouchWater = false
local Splash = false
RunService.Heartbeat:Connect(function()
local BodyPos = Player.Character.HumanoidRootPart.CFrame
local x, y, z = BodyPos.X, BodyPos.Y, BodyPos.Z
local min, max = Vector3.new(x + 0.01, y + 0.01, z + 0.01), Vector3.new(x - 0.01, y - 0.01, z - 0.01)
local region = Region3.new(max, min)
region = region:ExpandToGrid(4)
if region then
local material = workspace.Terrain:ReadVoxels(region, 4)
if material[1][1][1] == Enum.Material.Water then
TouchWater = true
else
TouchWater = false
end
end
end)
Humanoid.StateChanged:Connect(function(oldState, newState)
if newState == Enum.HumanoidStateType.Landed or oldState == Enum.HumanoidStateType.Freefall and newState == Enum.HumanoidStateType.Swimming or oldState == Enum.HumanoidStateType.Running and newState == Enum.HumanoidStateType.Swimming then
if TouchWater == true then
Splash = true
print(Splash)
wait(1)
Splash = false
print(Splash)
end
end
end)
spawn(function()
if Splash == true then
game.StarterPlayer.StarterCharacter.HumanoidRootPart.ParticleEmitter.Enabled = true
else
game.StarterPlayer.StarterCharacter.HumanoidRootPart.ParticleEmitter.Enabled = false
end
end)
the code works and changes ParticleEmmiter.Enabled fine :
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local RootPart = Character:WaitForChild("HumanoidRootPart")
local RunService = game:GetService("RunService")
local TouchWater = false
local Splash = false
RunService.Heartbeat:Connect(function()
local BodyPos = Player.Character.HumanoidRootPart.CFrame
local x, y, z = BodyPos.X, BodyPos.Y, BodyPos.Z
local min, max = Vector3.new(x + 0.01, y + 0.01, z + 0.01), Vector3.new(x - 0.01, y - 0.01, z - 0.01)
local region = Region3.new(max, min)
region = region:ExpandToGrid(4)
if region then
local material = workspace.Terrain:ReadVoxels(region, 4)
if material[1][1][1] == Enum.Material.Water then
TouchWater = true
else
TouchWater = false
end
end
end)
Humanoid.StateChanged:Connect(function(oldState, newState)
if newState == Enum.HumanoidStateType.Landed or oldState == Enum.HumanoidStateType.Freefall and newState == Enum.HumanoidStateType.Swimming or oldState == Enum.HumanoidStateType.Running and newState == Enum.HumanoidStateType.Swimming then
if TouchWater == true then
Splash = true
wait(1)
Splash = false
end
end
end)
spawn(function()
while wait() do
if Splash == true then
game.StarterPlayer.StarterCharacter.HumanoidRootPart.ParticleEmitter.Enabled = true
else
game.StarterPlayer.StarterCharacter.HumanoidRootPart.ParticleEmitter.Enabled = false
end
end
end)
When spawn(function)) is called why is there a while wait() do statement. If spawn is called every time the task scheduler is updated isn’t this irrelevant? I haven’t used it before so I’m just guessing.
Also, when the Heartbeat function is called and TouchWater is changed back to false won’t that cause the if TouchWater == true in the next function to fire again, changing Splash back to false almost instantly, which would cause the spawn function to turn Enabled back to false instantly too?
That’s why I asked you to put a print statement in the last function so you can see the time between when Enabled is turned to true then back to false to give you that information.
In Spawn((function)), you need an infinite loop because it’s just to do multiple infinite loops at the same time.
And I did what you told me it works perfectly, enabled = true we wait 1 sec then enabled = false.
The real problem is that I have a starting character and enabled is well defined but if we go to the player the Particle Emmiters.Enabled are not defined …